Ensure that it's possible to route the output type format by the requests stated accept header
()
| 253 | |
| 254 | |
| 255 | def test_accept(): |
| 256 | """Ensure that it's possible to route the output type format by the requests stated accept header""" |
| 257 | formatter = hug.output_format.accept( |
| 258 | {"application/json": hug.output_format.json, "text/plain": hug.output_format.text} |
| 259 | ) |
| 260 | |
| 261 | class FakeRequest(object): |
| 262 | accept = "application/json" |
| 263 | |
| 264 | request = FakeRequest() |
| 265 | response = FakeRequest() |
| 266 | converted = hug.input_format.json( |
| 267 | formatter(BytesIO(hug.output_format.json({"name": "name"})), request, response) |
| 268 | ) |
| 269 | assert converted == {"name": "name"} |
| 270 | |
| 271 | request.accept = "text/plain" |
| 272 | assert formatter("hi", request, response) == b"hi" |
| 273 | |
| 274 | request.accept = "application/json, text/plain; q=0.5" |
| 275 | assert formatter("hi", request, response) == b'"hi"' |
| 276 | |
| 277 | request.accept = "text/plain; q=0.5, application/json" |
| 278 | assert formatter("hi", request, response) == b'"hi"' |
| 279 | |
| 280 | request.accept = "application/json;q=0.4,text/plain; q=0.5" |
| 281 | assert formatter("hi", request, response) == b"hi" |
| 282 | |
| 283 | request.accept = "*" |
| 284 | assert formatter("hi", request, response) in [b'"hi"', b"hi"] |
| 285 | |
| 286 | request.accept = "undefined; always" |
| 287 | with pytest.raises(hug.HTTPNotAcceptable): |
| 288 | formatter("hi", request, response) |
| 289 | |
| 290 | formatter = hug.output_format.accept( |
| 291 | {"application/json": hug.output_format.json, "text/plain": hug.output_format.text}, |
| 292 | hug.output_format.json, |
| 293 | ) |
| 294 | assert formatter("hi", request, response) == b'"hi"' |
| 295 | |
| 296 | |
| 297 | def test_accept_with_http_errors(): |
nothing calls this directly
no test coverage detected