Пример #1
0
    async def test_rotation_because_of_reaching_limit(self):
        self.http.rotate_at = 1000  # this id the default
        responses = [
            (Request(method=b'get',
                     url='https://api.github.com/repos/ursa-labs/ursabot',
                     params=mock.ANY,
                     headers=HasHeaders({'Authorization': ['token A']}),
                     data=mock.ANY),
             Response(code=200,
                      headers={'X-RateLimit-Remaining': f'{i}'},
                      body=as_json({}))) for i in (1002, 1001, 1000)
        ] + [(Request(method=b'get',
                      url='https://api.github.com/rate_limit',
                      params=mock.ANY,
                      headers=HasHeaders({'Authorization': ['token B']}),
                      data=mock.ANY),
              Response(code=200,
                       headers={'X-RateLimit-Remaining': '5000'},
                       body=as_json({'rate': {
                           'remaining': 5000
                       }}))),
             (Request(method=b'get',
                      url='https://api.github.com/repos/ursa-labs/ursabot',
                      params=mock.ANY,
                      headers=HasHeaders({'Authorization': ['token B']}),
                      data=mock.ANY),
              Response(code=200,
                       headers={'X-RateLimit-Remaining': '4999'},
                       body=as_json({})))]

        with self.responses(responses):
            for _ in range(4):
                await self.http.get('/repos/ursa-labs/ursabot')
Пример #2
0
 async def test_fetching_rate_limit(self):
     responses = [
         (Request(method=b'get',
                  url='https://api.github.com/rate_limit',
                  params=mock.ANY,
                  headers=HasHeaders({'Authorization': ['token A']}),
                  data=mock.ANY),
          Response(code=200,
                   headers={'X-RateLimit-Remaining': '5000'},
                   body=as_json({'rate': {
                       'remaining': 5000
                   }}))),
         (Request(method=b'get',
                  url='https://api.github.com/rate_limit',
                  params=mock.ANY,
                  headers=HasHeaders({'Authorization': ['token B']}),
                  data=mock.ANY),
          Response(code=200,
                   headers={'X-RateLimit-Remaining': '4000'},
                   body=as_json({'rate': {
                       'remaining': 4000
                   }})))
     ]
     with self.responses(responses):
         assert await self.http.rate_limit('A') == 5000
         assert await self.http.rate_limit('B') == 4000
Пример #3
0
    def test_mismatched_request_causes_failure(self):
        """
        If a request is made that is not expected as the next request,
        causes a failure.
        """
        sequence = RequestSequence(
            [(('get', 'https://anything/', {'1': ['2']},
               HasHeaders({'1': ['1']}), 'what'),
              (418, {}, 'body')),
             (('get', 'http://anything', {}, HasHeaders({'2': ['1']}), 'what'),
              (202, {}, 'deleted'))],
            async_failure_reporter=self.async_failures.append)

        stub = StubTreq(StringStubbingResource(sequence))
        get = partial(stub.get, 'https://anything?1=2', data='what',
                      headers={'1': '1'})

        resp = self.successResultOf(get())
        self.assertEqual(418, resp.code)
        self.assertEqual('body', self.successResultOf(stub.content(resp)))
        self.assertEqual([], self.async_failures)

        resp = self.successResultOf(get())
        self.assertEqual(500, resp.code)
        self.assertEqual(1, len(self.async_failures))
        self.assertIn("Expected the next request to be",
                      self.async_failures[0])

        self.assertFalse(sequence.consumed())
Пример #4
0
    def test_bytes_encoded_forms(self):
        """
        The :obj:`HasHeaders` equality function compares the bytes-encoded
        forms of both sets of headers.
        """
        self.assertEqual(HasHeaders({b'a': [b'a']}), {u'a': [u'a']})

        self.assertEqual(HasHeaders({u'b': [u'b']}), {b'b': [b'b']})
Пример #5
0
    async def test_rotation_becasue_of_forbidden_access(self):
        from treq.testing import HasHeaders

        self.http.rotate_at = 1000  # this id the default
        responses = [
            (Request(method=b'get',
                     url='https://api.github.com/repos/ursa-labs/ursabot',
                     params=mock.ANY,
                     headers=HasHeaders({'Authorization': ['token A']}),
                     data=mock.ANY),
             Response(code=403,
                      headers={'X-RateLimit-Remaining': '0'},
                      body=as_json({}))),
            (Request(method=b'get',
                     url='https://api.github.com/rate_limit',
                     params=mock.ANY,
                     headers=HasHeaders({'Authorization': ['token B']}),
                     data=mock.ANY),
             Response(code=200,
                      headers={'X-RateLimit-Remaining': '900'},
                      body=as_json({'rate': {
                          'remaining': 900
                      }}))),
            (Request(method=b'get',
                     url='https://api.github.com/rate_limit',
                     params=mock.ANY,
                     headers=HasHeaders({'Authorization': ['token C']}),
                     data=mock.ANY),
             Response(code=200,
                      headers={'X-RateLimit-Remaining': '5000'},
                      body=as_json({'rate': {
                          'remaining': 5000
                      }}))),
            (Request(method=b'get',
                     url='https://api.github.com/repos/ursa-labs/ursabot',
                     params=mock.ANY,
                     headers=HasHeaders({'Authorization': ['token C']}),
                     data=mock.ANY),
             Response(code=200,
                      headers={'X-RateLimit-Remaining': '4999'},
                      body=as_json({}))),
            (Request(method=b'get',
                     url='https://api.github.com/repos/ursa-labs/ursabot',
                     params=mock.ANY,
                     headers=HasHeaders({'Authorization': ['token C']}),
                     data=mock.ANY),
             Response(code=200,
                      headers={'X-RateLimit-Remaining': '4998'},
                      body=as_json({})))
        ]

        with self.responses(responses):
            await self.http.get('/repos/ursa-labs/ursabot')
            await self.http.get('/repos/ursa-labs/ursabot')
Пример #6
0
 def test_equality_and_strict_subsets_succeed(self):
     """
     The :obj:`HasHeaders` returns True if both sets of headers are
     equivalent, or the first is a strict subset of the second.
     """
     self.assertEqual(HasHeaders({'one': ['two', 'three']}),
                      {'one': ['two', 'three']},
                      "Equivalent headers do not match.")
     self.assertEqual(HasHeaders({'one': ['two', 'three']}),
                      {'one': ['two', 'three', 'four'],
                       'ten': ['six']},
                      "Strict subset headers do not match")
Пример #7
0
 def test_partial_or_zero_intersection_subsets_fail(self):
     """
     The :obj:`HasHeaders` returns False if both sets of headers overlap
     but the first is not a strict subset of the second.  It also returns
     False if there is no overlap.
     """
     self.assertNotEqual(HasHeaders({'one': ['two', 'three']}),
                         {'one': ['three', 'four']},
                         "Partial value overlap matches")
     self.assertNotEqual(HasHeaders({'one': ['two', 'three']}),
                         {'one': ['two']}, "Missing value matches")
     self.assertNotEqual(HasHeaders({'one': ['two', 'three']}),
                         {'ten': ['six']}, "Complete inequality matches")
Пример #8
0
 def test_case_insensitive_keys(self):
     """
     The :obj:`HasHeaders` equality function ignores the case of the header
     keys.
     """
     self.assertEqual(HasHeaders({'A': ['1'], 'b': ['2']}),
                      {'a': ['1'], 'B': ['2']})
Пример #9
0
 def setup_treq(self, code=200, body={}):
     self.async_failures = []
     self.stubs = RequestSequence(
         [((b"get", "http://server:8989/index", {},
            HasHeaders({"Bloc-Session-ID": ["sid"]}), b''),
           (code, {}, json.dumps(body).encode("utf-8")))],
         self.async_failures.append)
     self.client.treq = StubTreq(StringStubbingResource(self.stubs))
Пример #10
0
 def test_repr(self):
     """
     :obj:`HasHeaders` returns a nice string repr.
     """
     self.assertEqual(
         "HasHeaders({b'a': [b'b']})",
         repr(HasHeaders({b"A": [b"b"]})),
     )
Пример #11
0
 def test_repr(self):
     """
     :obj:`HasHeaders` returns a nice string repr.
     """
     if _PY3:
         reprOutput = "HasHeaders({b'a': [b'b']})"
     else:
         reprOutput = "HasHeaders({'a': ['b']})"
     self.assertEqual(reprOutput, repr(HasHeaders({b'A': [b'b']})))
Пример #12
0
 def _requestSequenceGenerator(self, response, statusCode=http.OK):
     return RequestSequence(
         [((b'get', 'https://k8s.wmnet.test:1234/v1/nodes', {
             b'pretty': [b'false']
         },
            HasHeaders({
                'Accept': ['application/json'],
                'Authorization': ['Bearer token'],
            }), b''), (statusCode, {
                b'Content-Type': b'application/json'
            }, response))], log.error)
Пример #13
0
    def test_200_ok(self):
        """On a 200 response, return the response's JSON."""
        req_seq = RequestSequence([
            ((b'get', 'http://an.example/foo', {b'a': [b'b']},
              HasHeaders({'Accept': ['application/json']}), b''),
             (http.OK, {b'Content-Type': b'application/json'}, b'{"status": "ok"}'))
        ], log.error)
        treq = StubTreq(StringStubbingResource(req_seq))

        with req_seq.consume(self.fail):
            result = self.successResultOf(make_a_request(treq))

        self.assertEqual({"status": "ok"}, result)
Пример #14
0
 async def test_basic(self):
     responses = [
         (Request(method=b'get',
                  url='https://api.github.com/repos/ursa-labs/ursabot',
                  params=mock.ANY,
                  headers=HasHeaders({'Authorization': ['token A']}),
                  data=mock.ANY),
          Response(code=200,
                   headers={'X-RateLimit-Remaining': '5000'},
                   body=as_json({})))
     ]
     with self.responses(responses):
         await self.http.get('/repos/ursa-labs/ursabot')
Пример #15
0
    def test_418_teapot(self):
        """On an unexpected response code, raise an exception"""
        req_seq = RequestSequence([
            ((b'get', 'http://an.example/foo', {b'a': [b'b']},
              HasHeaders({'Accept': ['application/json']}), b''),
             (418, {b'Content-Type': b'text/plain'}, b"I'm a teapot!"))
        ], log.error)
        treq = StubTreq(StringStubbingResource(req_seq))

        with req_seq.consume(self.fail):
            failure = self.failureResultOf(make_a_request(treq))

        self.assertEqual(u"Got an error from the server: I'm a teapot!",
                         failure.getErrorMessage())
Пример #16
0
 def test_stopservice_deletes_session(self):
     """
     :func:`stopService` will delete the session and will stop the loop
     """
     self.test_settled()
     stubs = RequestSequence(
         [((b"delete", "http://server:8989/session", {},
            HasHeaders({"Bloc-Session-ID": ["sid"]}), b''),
           (200, {}, b''))],
         self.fail)
     self.client.treq = StubTreq(StringStubbingResource(stubs))
     with stubs.consume(self.fail):
         d = self.client.stopService()
         self.assertIsNone(self.successResultOf(d))
         # Moving time would fail treq if it tried to heartbeat
         self.clock.advance(4)
Пример #17
0
 def test_repr(self):
     """
     :obj:`HasHeaders` returns a nice string repr.
     """
     self.assertEqual("HasHeaders({'a': ['b']})",
                      repr(HasHeaders({'A': ['b']})))
Пример #18
0
 def test_case_sensitive_values(self):
     """
     The :obj:`HasHeaders` equality function does care about the case of
     the header value.
     """
     self.assertNotEqual(HasHeaders({'a': ['a']}), {'a': ['A']})
Пример #19
0
 def get_response_for(_method, _url, _params, _headers, _data):
     self.assertEqual((method, url, params, data),
                      (_method, _url, _params, _data))
     self.assertEqual(HasHeaders(headers), _headers)
     return response