def test_transport_handler(self, transport):
        global_env = {}

        def request_handler(r):
            global_env["req"] = r
            return r

        def response_handler(r):
            global_env["resp"] = r
            return r

        transport.middleware.request(handler=request_handler)
        transport.middleware.response(handler=response_handler)

        req = Request(
            url="http://httpbin.org/anything",
            method="post",
            json={"foo": "bar"},
        )
        resp = transport.send(req)
        assert resp.text
        assert resp.json()["json"] == {"foo": "bar"}

        assert "req" in global_env
        assert "resp" in global_env
 def test_transport_send(self, transport):
     req = Request(
         url="http://httpbin.org/anything",
         method="post",
         json={"foo": "bar"},
     )
     resp = transport.send(req)
     assert resp.text
     assert resp.json()["json"] == {"foo": "bar"}
示例#3
0
    def send(self, req: Request, **options: dict) -> Response:
        resp = Response(req.url, req.method)
        for handler in self.transport_handlers:
            resp = handler(req)

        for handler in self.client_handler:
            payload = handler(req.payload())
            resp.content = json.dumps(payload).encode("utf-8")

        return resp
示例#4
0
def test_transport_handler(transport):
    req_key, resp_key, exc_key = "req", "resp", "exc"
    counter = Counter({req_key: 0, resp_key: 0, exc_key: 0})

    def request_handler(r):
        counter[req_key] += 1
        return r

    def response_handler(r):
        counter[resp_key] += 1
        return r

    def exception_handler(r):
        counter[exc_key] += 1
        return r

    transport.middleware.request(handler=request_handler)
    transport.middleware.response(handler=response_handler)
    transport.middleware.exception(handler=exception_handler)

    expect = {"foo": "bar"}
    req = Request(url=TEST_URL, method="post", json=expect)

    with requests_mock.Mocker() as m:
        request_uuid = str(uuid.uuid4())
        m.post(
            TEST_URL,
            text=json.dumps(expect),
            status_code=200,
            headers={http.REQUEST_UUID_HEADER_KEY: request_uuid},
        )
        resp = transport.send(req)
        assert resp.text
        assert resp.json() == expect
        assert resp.request_uuid == request_uuid

    with pytest.raises(Exception):
        transport.send(Request(url="/"))

    assert counter[req_key] == 2
    assert counter[resp_key] == 1
    assert counter[exc_key] == 1
示例#5
0
 def _build_http_request(self, args):
     payload = {
         "Region": self.config.region,
         "ProjectId": self.config.project_id
     }
     payload.update({k: v for k, v in args.items() if v is not None})
     payload["Signature"] = self.credential.verify_ac(payload)
     return Request(
         url=self.config.base_url,
         method="post",
         json=payload,
         headers={
             "User-Agent": self._build_user_agent(),
             "Content-Type": "application/json",
         },
     )
示例#6
0
def test_transport(transport, status_code, content, expect, expect_exc,
                   retryable):
    with requests_mock.Mocker() as m:
        m.post(TEST_URL, text=content, status_code=status_code)

        got_exc = None
        try:
            resp = transport.send(Request(url=TEST_URL, method="post",
                                          json={}))
            assert resp.json() == expect
        except Exception as e:
            got_exc = e

        if expect_exc:
            assert str(got_exc)
            assert got_exc.retryable == retryable
            assert isinstance(got_exc, expect_exc)
示例#7
0
    def _build_http_request(self, args: dict) -> Request:
        config = {
            "Region": self.config.region,
            "ProjectId": self.config.project_id,
        }
        payload = {k: v for k, v in config.items() if v is not None}
        payload.update({k: v for k, v in args.items() if v is not None})
        payload = encoder.encode(payload)
        payload["Signature"] = self.credential.verify_ac(payload)

        return Request(
            url=self.config.base_url,
            method="post",
            data=payload,
            headers={
                "User-Agent": self._build_user_agent(),
                "Content-Type": "application/x-www-form-urlencoded",
            },
        )
示例#8
0
    def _build_request(self, action: str, data: dict = None) -> Request:
        payload = {
            "Region": self.config.region,
            "ProjectId": self.config.project_id
        }
        payload.update(
            {k: v
             for k, v in (data or {}).items()
             if v is not None})  # overwrite region and project id
        payload["Action"] = action  # overwrite action
        payload["Signature"] = self.credential.verify_ac(payload)

        return Request(
            url=self.config.base_url,
            method="post",
            json=payload,
            headers={
                "User-Agent": self._build_user_agent(),
                "Content-Type": "application/json",
            },
        )
示例#9
0
def test_request_methods():
    req = Request(TEST_URL,
                  data={"foo": 42},
                  json={"bar": 42},
                  params={"q": "search"})
    assert req.payload() == {"foo": 42, "bar": 42, "q": "search"}