Esempio n. 1
0
 def app(environ, start_response):
     request = Request(environ)
     body = b""
     for chunk in request.stream():
         body += chunk
     response = PlainTextResponse(body)
     return response(environ, start_response)
Esempio n. 2
0
 def app(environ, start_response):
     request = Request(environ)
     if request.accepts("application/json"):
         response = JSONResponse({"data": data})
     else:
         response = PlainTextResponse(data)
     return response(environ, start_response)
Esempio n. 3
0
 def app(environ, start_response):
     request = Request(environ)
     form = request.form
     file = form["file-key"]
     assert isinstance(file, UploadFile)
     assert file.read() == b"temporary file"
     response = JSONResponse({"file": file.filename})
     request.close()
     return response(environ, start_response)
Esempio n. 4
0
def app_read_body(environ, start_response):
    request = Request(environ)
    data = request.form
    output = {}
    for key, value in data.items():
        output[key] = value
    request.close()
    response = JSONResponse(output)
    return response(environ, start_response)
Esempio n. 5
0
 def app(environ, start_response):
     request = Request(environ)
     body = request.body
     chunks = b""
     for chunk in request.stream():
         chunks += chunk
     response = JSONResponse({
         "body": body.decode(),
         "stream": chunks.decode()
     })
     return response(environ, start_response)
Esempio n. 6
0
 def app(environ, start_response):
     request = Request(environ)
     chunks = b""
     for chunk in request.stream():
         chunks += chunk
     try:
         body = request.body
     except RuntimeError:
         body = b"<stream consumed>"
     response = JSONResponse({
         "body": body.decode(),
         "stream": chunks.decode()
     })
     return response(environ, start_response)
Esempio n. 7
0
 def app(environ, start_response):
     request = Request(environ)
     response = JSONResponse({
         "host": request.client.host,
         "port": request.client.port
     })
     return response(environ, start_response)
Esempio n. 8
0
def app(environ, start_response):
    request = Request(environ)
    data = request.form
    output = {}
    for key, value in data.items():
        if isinstance(value, UploadFile):
            content = value.read()
            output[key] = {
                "filename": value.filename,
                "content": content.decode(),
                "content_type": value.content_type,
            }
        else:
            output[key] = value
    request.close()
    response = JSONResponse(output)
    return response(environ, start_response)
Esempio n. 9
0
def multi_items_app(environ, start_response):
    request = Request(environ)
    data = request.form
    output = {}
    for key, value in data.multi_items():
        if key not in output:
            output[key] = []
        if isinstance(value, UploadFile):
            content = value.read()
            output[key].append({
                "filename": value.filename,
                "content": content.decode(),
                "content_type": value.content_type,
            })
        else:
            output[key].append(value)
    request.close()
    response = JSONResponse(output)
    return response(environ, start_response)
Esempio n. 10
0
def test_request_environ_interface():
    """
    A Request can be instantiated with a environ, and presents a `Mapping`
    interface.
    """
    request = Request({"type": "http", "method": "GET", "path": "/abc/"})
    assert request["method"] == "GET"
    assert dict(request) == {"type": "http", "method": "GET", "path": "/abc/"}
    assert len(request) == 3
    # test eq
    assert request == Request({
        "type": "http",
        "method": "GET",
        "path": "/abc/"
    })
    assert request != Request({
        "type": "http",
        "method": "GET",
        "path": "/abc/",
        "query_params": {}
    })
    assert request != dict({"type": "http", "method": "GET", "path": "/abc/"})
Esempio n. 11
0
def test_request_client():
    def app(environ, start_response):
        request = Request(environ)
        response = JSONResponse({
            "host": request.client.host,
            "port": request.client.port
        })
        return response(environ, start_response)

    with httpx.Client(app=app, base_url="http://testServer/") as client:
        response = client.get("/")
        assert response.json() == {"host": None, "port": None}

    request = Request({"REMOTE_ADDR": "127.0.0.1", "REMOTE_PORT": "62124"})
    assert request.client == Address("127.0.0.1", 62124)
Esempio n. 12
0
 def app(environ, start_response):
     request = Request(environ)
     form = request.form
     response = JSONResponse({"form": dict(form)})
     return response(environ, start_response)
     request.close()
Esempio n. 13
0
 def app(environ, start_response):
     request = Request(environ)
     params = dict(request.query_params)
     response = JSONResponse({"params": params})
     return response(environ, start_response)
Esempio n. 14
0
 def app(environ, start_response):
     request = Request(environ)
     headers = dict(request.headers)
     headers.pop("user-agent")  # this is httpx version, delete it
     response = JSONResponse({"headers": headers})
     return response(environ, start_response)
Esempio n. 15
0
 def app(environ, start_response):
     request = Request(environ)
     response = PlainTextResponse(request.method + " " + str(request.url))
     return response(environ, start_response)
Esempio n. 16
0
 def path(request: Request) -> Response:
     return PlainTextResponse(request.get("PATH_INFO", ""))
Esempio n. 17
0
 def root(request: Request) -> Response:
     return PlainTextResponse(request.get("SCRIPT_NAME", ""))
Esempio n. 18
0
 def app(environ, start_response):
     request = Request(environ)
     body = request.body
     response = JSONResponse({"body": body.decode()})
     return response(environ, start_response)
Esempio n. 19
0
 def app(environ, start_response):
     request = Request(environ)
     data = request.json
     response = JSONResponse({"json": data})
     return response(environ, start_response)