Skip to content

Commit

Permalink
Support more JSON / XML "like" mimetypes with debug output
Browse files Browse the repository at this point in the history
  • Loading branch information
jcamiel committed Oct 30, 2024
1 parent 86a7b6b commit cfe0e81
Show file tree
Hide file tree
Showing 4 changed files with 843 additions and 14 deletions.
89 changes: 89 additions & 0 deletions integration/hurl/tests_failed/error_format_long.err.pattern
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,92 @@ error: Assert failure
| expected: string <You have enough credit.>
|

HTTP/1.1 200
Server: Werkzeug/<<<.*?>>> Python/<<<.*?>>>
Date: <<<.*?>>>
Content-Type: application/fhir+json
Content-Length: 902
Server: Flask Server
Connection: close

{"resourceType": "Practitioner", "id": "example", "text": {"status": "generated", "div": "<div xmlns=\"http://www.w3.org/1999/xhtml\">\n <p>Dr Adam Careful is a Referring Practitioner for Acme Hospital from 1-Jan 2012 to 31-Mar\n 2012</p>\n </div>"}, "identifier": [{"system": "http://www.acme.org/practitioners", "value": "23"}], "active": true, "name": [{"family": "Careful", "given": ["Adam"], "prefix": ["Dr"]}], "address": [{"use": "home", "line": ["534 Erewhon St"], "city": "PleasantVille", "state": "Vic", "postalCode": "3999"}], "qualification": [{"identifier": [{"system": "http://example.org/UniversityIdentifier", "value": "12345"}], "code": {"coding": [{"system": "http://terminology.hl7.org/CodeSystem/v2-0360/2.7", "code": "BS", "display": "Bachelor of Science"}], "text": "Bachelor of Science"}, "period": {"start": "1995"}, "issuer": {"display": "Example University"}}]}

error: Assert failure
--> tests_failed/error_format_long.hurl:34:0
|
| GET http://localhost:8000/error-format-long/fhir
| ...
34 | jsonpath "$.id" == "foo"
| actual: string <example>
| expected: string <foo>
|

HTTP/1.1 200
Server: Werkzeug/<<<.*?>>> Python/<<<.*?>>>
Date: <<<.*?>>>
Content-Type: text/csv; charset=utf-8
Content-Length: 726
Server: Flask Server
Connection: close

"Year","Score","Title"
1968,86,"Greetings"
1970,17,"Bloody Mama"
1970,73,"Hi, Mom!"
1971,40,"Born to Win"
1973,98,"Mean Streets"
1973,88,"Bang the Drum Slowly"
1974,97,"The Godfather, Part II"
1976,41,"The Last Tycoon"
1976,99,"Taxi Driver"
1977,47,"1900"
1977,67,"New York, New York"
1978,93,"The Deer Hunter"
1980,97,"Raging Bull"
1981,75,"True Confessions"
1983,90,"The King of Comedy"
1984,89,"Once Upon a Time in America"
1984,60,"Falling in Love"
1985,98,"Brazil"
1986,65,"The Mission"
1987,00,"Dear America: Letters Home From Vietnam"
1987,80,"The Untouchables"
1987,78,"Angel Heart"
1988,96,"Midnight Run"
1989,64,"Jacknife"
1989,47,"We're No Angels"
1990,88,"Awakenings"
1990,29,"Stanley & Iris"
1990,96,"Goodfellas"


error: Assert failure
--> tests_failed/error_format_long.hurl:42:0
|
| GET http://localhost:8000/error-format-long/csv
| ...
42 | body split "\n" nth 9 split "," nth 2 == "\"Taxi\""
| actual: string <"Taxi Driver">
| expected: string <"Taxi">
|

HTTP/1.1 200
Server: Werkzeug/<<<.*?>>> Python/<<<.*?>>>
Date: <<<.*?>>>
Content-Type: application/octet-stream
Content-Length: 4
Server: Flask Server
Connection: close

Bytes <deadbeef...>

error: Assert failure
--> tests_failed/error_format_long.hurl:50:0
|
| GET http://localhost:8000/error-format-long/bytes
| ...
50 | bytes == hex,beef;
| actual: byte array <deadbeef>
| expected: byte array <beef>
|

24 changes: 24 additions & 0 deletions integration/hurl/tests_failed/error_format_long.hurl
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,27 @@ HTTP 200
Content-Type: application/problem+json
[Asserts]
jsonpath "$.title" == "You have enough credit."


# "JSON like" mimetype are supported, like [FHIR](https://www.hl7.org/fhir/json.html)
GET http://localhost:8000/error-format-long/fhir
HTTP 200
Content-Type: application/fhir+json
[Asserts]
jsonpath "$.id" == "foo"


# Text like mimetype like CSV are supported
GET http://localhost:8000/error-format-long/csv
HTTP 200
Content-Type: text/csv; charset=utf-8
[Asserts]
body split "\n" nth 9 split "," nth 2 == "\"Taxi\""


# Bytestream are dumped as a raw bytes stream
GET http://localhost:8000/error-format-long/bytes
HTTP 200
Content-Type: application/octet-stream
[Asserts]
bytes == hex,beef;
96 changes: 95 additions & 1 deletion integration/hurl/tests_failed/error_format_long.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import json
from io import BytesIO

from app import app
from flask import Response
from flask import Response, make_response


@app.route("/error-format-long/html")
Expand Down Expand Up @@ -37,3 +38,96 @@ def error_format_problem_json():
"accounts": ["/account/12345", "/account/67890"],
}
return Response(json.dumps(data), mimetype="application/problem+json")


@app.route("/error-format-long/fhir")
def error_format_fhir_json():
data = {
"resourceType": "Practitioner",
"id": "example",
"text": {
"status": "generated",
"div": '<div xmlns="http://www.w3.org/1999/xhtml">\n <p>Dr Adam Careful is a Referring Practitioner for Acme Hospital from 1-Jan 2012 to 31-Mar\n 2012</p>\n </div>',
},
"identifier": [{"system": "http://www.acme.org/practitioners", "value": "23"}],
"active": True,
"name": [{"family": "Careful", "given": ["Adam"], "prefix": ["Dr"]}],
"address": [
{
"use": "home",
"line": ["534 Erewhon St"],
"city": "PleasantVille",
"state": "Vic",
"postalCode": "3999",
}
],
"qualification": [
{
"identifier": [
{
"system": "http://example.org/UniversityIdentifier",
"value": "12345",
}
],
"code": {
"coding": [
{
"system": "http://terminology.hl7.org/CodeSystem/v2-0360/2.7",
"code": "BS",
"display": "Bachelor of Science",
}
],
"text": "Bachelor of Science",
},
"period": {"start": "1995"},
"issuer": {"display": "Example University"},
}
],
}
return Response(json.dumps(data), mimetype="application/fhir+json")


@app.route("/error-format-long/bytes")
def error_format_bytes():
result = BytesIO()
result.write(b"\xde\xad\xbe\xef")
data = result.getvalue()
resp = make_response(data)
resp.content_type = "application/octet-stream"
return resp


@app.route("/error-format-long/csv")
def error_format_csv():
data = """\
"Year","Score","Title"
1968,86,"Greetings"
1970,17,"Bloody Mama"
1970,73,"Hi, Mom!"
1971,40,"Born to Win"
1973,98,"Mean Streets"
1973,88,"Bang the Drum Slowly"
1974,97,"The Godfather, Part II"
1976,41,"The Last Tycoon"
1976,99,"Taxi Driver"
1977,47,"1900"
1977,67,"New York, New York"
1978,93,"The Deer Hunter"
1980,97,"Raging Bull"
1981,75,"True Confessions"
1983,90,"The King of Comedy"
1984,89,"Once Upon a Time in America"
1984,60,"Falling in Love"
1985,98,"Brazil"
1986,65,"The Mission"
1987,00,"Dear America: Letters Home From Vietnam"
1987,80,"The Untouchables"
1987,78,"Angel Heart"
1988,96,"Midnight Run"
1989,64,"Jacknife"
1989,47,"We're No Angels"
1990,88,"Awakenings"
1990,29,"Stanley & Iris"
1990,96,"Goodfellas"
"""
return Response(data, mimetype="text/csv")
Loading

0 comments on commit cfe0e81

Please sign in to comment.