Esempio n. 1
0
 def app(request):
     self.assertEqual(request.method, "POST")
     self.assertEqual(request.path, "/api/uploader")
     body = request.get_data()
     request_pb = server_info_pb2.ServerInfoRequest.FromString(body)
     self.assertEqual(request_pb.version, version.VERSION)
     self.assertEqual(request_pb.plugin_specification.upload_plugins,
                      [])
     return wrappers.BaseResponse(expected_result.SerializeToString())
Esempio n. 2
0
 def test_secure(self):
     response = wrappers.BaseResponse()
     response.set_cookie('foo', value='bar', max_age=60, expires=0,
                         path='/blub', domain='example.org', secure=True)
     strict_eq(response.headers.to_wsgi_list(), [
         ('Content-Type', 'text/plain; charset=utf-8'),
         ('Set-Cookie', 'foo=bar; Domain=example.org; Expires=Thu, '
          '01-Jan-1970 00:00:00 GMT; Max-Age=60; Secure; Path=/blub')
     ])
Esempio n. 3
0
    def test_base_response(self):
        # unicode
        response = wrappers.BaseResponse(u'öäü')
        assert response.data == 'öäü'

        # writing
        response = wrappers.Response('foo')
        response.stream.write('bar')
        assert response.data == 'foobar'

        # set cookie
        response = wrappers.BaseResponse()
        response.set_cookie('foo', 'bar', 60, 0, '/blub', 'example.org', False)
        assert response.headers.to_list() == [
            ('Content-Type', 'text/plain; charset=utf-8'),
            ('Set-Cookie', 'foo=bar; Domain=example.org; expires=Thu, '
             '01-Jan-1970 00:00:00 GMT; Max-Age=60; Path=/blub')
        ]

        # delete cookie
        response = wrappers.BaseResponse()
        response.delete_cookie('foo')
        assert response.headers.to_list() == [
            ('Content-Type', 'text/plain; charset=utf-8'),
            ('Set-Cookie', 'foo=; expires=Thu, 01-Jan-1970 00:00:00 GMT; Max-Age=0; Path=/')
        ]

        # close call forwarding
        closed = []
        class Iterable(object):
            def next(self):
                raise StopIteration()
            def __iter__(self):
                return self
            def close(self):
                closed.append(True)
        response = wrappers.BaseResponse(Iterable())
        response.call_on_close(lambda: closed.append(True))
        app_iter, status, headers = run_wsgi_app(response,
                                                 create_environ(),
                                                 buffered=True)
        assert status == '200 OK'
        assert ''.join(app_iter) == ''
        assert len(closed) == 2
Esempio n. 4
0
 def app(request):
     body = request.get_data()
     request_pb = server_info_pb2.ServerInfoRequest.FromString(body)
     self.assertEqual(request_pb.version, version.VERSION)
     self.assertEqual(
         request_pb.plugin_specification.upload_plugins,
         ["plugin1", "plugin2"],
     )
     return wrappers.BaseResponse(
         server_info_pb2.ServerInfoResponse().SerializeToString())
Esempio n. 5
0
 def test_response_status_codes(self):
     response = wrappers.BaseResponse()
     response.status_code = 404
     assert response.status == '404 NOT FOUND'
     response.status = '200 OK'
     assert response.status_code == 200
     response.status = '999 WTF'
     assert response.status_code == 999
     response.status_code = 588
     assert response.status_code == 588
     assert response.status == '588 UNKNOWN'
     response.status = 'wtf'
     assert response.status_code == 0
Esempio n. 6
0
 def test_response_status_codes(self):
     response = wrappers.BaseResponse()
     response.status_code = 404
     self.assert_strict_equal(response.status, '404 NOT FOUND')
     response.status = '200 OK'
     self.assert_strict_equal(response.status_code, 200)
     response.status = '999 WTF'
     self.assert_strict_equal(response.status_code, 999)
     response.status_code = 588
     self.assert_strict_equal(response.status_code, 588)
     self.assert_strict_equal(response.status, '588 UNKNOWN')
     response.status = 'wtf'
     self.assert_strict_equal(response.status_code, 0)
Esempio n. 7
0
def tf_profile_plugin_mock():
    """Mock the tensorboard profile plugin"""
    import tensorboard_plugin_profile.profile_plugin

    with mock.patch.object(
            tensorboard_plugin_profile.profile_plugin.ProfilePlugin,
            "capture_route") as profile_mock:
        profile_mock.return_value = (wrappers.BaseResponse(
            json.dumps({"error": "some error"}),
            content_type="application/json",
            status=200,
        ), )
        yield profile_mock
Esempio n. 8
0
 def test_response_status_codes(self):
     response = wrappers.BaseResponse()
     response.status_code = 404
     self.assert_strict_equal(response.status, '404 Not Found')
     response.status = '200 OK'
     self.assert_strict_equal(response.status_code, 200)
     response.status = '999 WTF'
     self.assert_strict_equal(response.status_code, 999)
     response.status_code = 588
     self.assert_strict_equal(response.status_code, 588)
     self.assert_strict_equal(response.status, '588 Unknown')
     response.status = 'wtf'
     self.assert_strict_equal(response.status_code, 0)
     self.assert_strict_equal(response.status, '0 wtf')
Esempio n. 9
0
def test_response_status_codes():
    response = wrappers.BaseResponse()
    response.status_code = 404
    strict_eq(response.status, '404 NOT FOUND')
    response.status = '200 OK'
    strict_eq(response.status_code, 200)
    response.status = '999 WTF'
    strict_eq(response.status_code, 999)
    response.status_code = 588
    strict_eq(response.status_code, 588)
    strict_eq(response.status, '588 UNKNOWN')
    response.status = 'wtf'
    strict_eq(response.status_code, 0)
    strict_eq(response.status, '0 wtf')
Esempio n. 10
0
def test_response_status_codes():
    response = wrappers.BaseResponse()
    response.status_code = 404
    strict_eq(response.status, "404 NOT FOUND")
    response.status = "200 OK"
    strict_eq(response.status_code, 200)
    response.status = "999 WTF"
    strict_eq(response.status_code, 999)
    response.status_code = 588
    strict_eq(response.status_code, 588)
    strict_eq(response.status, "588 UNKNOWN")
    response.status = "wtf"
    strict_eq(response.status_code, 0)
    strict_eq(response.status, "0 wtf")

    # invalid status codes
    with pytest.raises(ValueError) as empty_string_error:
        wrappers.BaseResponse(None, "")
    assert "Empty status argument" in str(empty_string_error)

    with pytest.raises(TypeError) as invalid_type_error:
        wrappers.BaseResponse(None, tuple())
    assert "Invalid status argument" in str(invalid_type_error)
Esempio n. 11
0
def test_response_status_codes():
    response = wrappers.BaseResponse()
    response.status_code = 404
    strict_eq(response.status, '404 NOT FOUND')
    response.status = '200 OK'
    strict_eq(response.status_code, 200)
    response.status = '999 WTF'
    strict_eq(response.status_code, 999)
    response.status_code = 588
    strict_eq(response.status_code, 588)
    strict_eq(response.status, '588 UNKNOWN')
    response.status = 'wtf'
    strict_eq(response.status_code, 0)
    strict_eq(response.status, '0 wtf')

    # invalid status codes
    with pytest.raises(ValueError) as empty_string_error:
        wrappers.BaseResponse(None, '')
    assert 'Empty status argument' in str(empty_string_error)

    with pytest.raises(TypeError) as invalid_type_error:
        wrappers.BaseResponse(None, tuple())
    assert 'Invalid status argument' in str(invalid_type_error)
Esempio n. 12
0
 def test_secure(self):
     response = wrappers.BaseResponse()
     response.set_cookie(
         "foo",
         value="bar",
         max_age=60,
         expires=0,
         path="/blub",
         domain="example.org",
         secure=True,
         samesite=None,
     )
     strict_eq(
         response.headers.to_wsgi_list(),
         [
             ("Content-Type", "text/plain; charset=utf-8"),
             (
                 "Set-Cookie",
                 "foo=bar; Domain=example.org; Expires=Thu, "
                 "01-Jan-1970 00:00:00 GMT; Max-Age=60; Secure; Path=/blub",
             ),
         ],
     )
Esempio n. 13
0
    def test_type_forcing(self):
        def wsgi_application(environ, start_response):
            start_response('200 OK', [('Content-Type', 'text/html')])
            return ['Hello World!']
        base_response = wrappers.BaseResponse('Hello World!', content_type='text/html')

        class SpecialResponse(wrappers.Response):
            def foo(self):
                return 42

        # good enough for this simple application, but don't ever use that in
        # real world examples!
        fake_env = {}

        for orig_resp in wsgi_application, base_response:
            response = SpecialResponse.force_type(orig_resp, fake_env)
            assert response.__class__ is SpecialResponse
            self.assert_strict_equal(response.foo(), 42)
            self.assert_strict_equal(response.get_data(), b'Hello World!')
            self.assert_equal(response.content_type, 'text/html')

        # without env, no arbitrary conversion
        self.assert_raises(TypeError, SpecialResponse.force_type, wsgi_application)
Esempio n. 14
0
def test_type_forcing():
    def wsgi_application(environ, start_response):
        start_response("200 OK", [("Content-Type", "text/html")])
        return ["Hello World!"]

    base_response = wrappers.BaseResponse("Hello World!", content_type="text/html")

    class SpecialResponse(wrappers.Response):
        def foo(self):
            return 42

    # good enough for this simple application, but don't ever use that in
    # real world examples!
    fake_env = {}

    for orig_resp in wsgi_application, base_response:
        response = SpecialResponse.force_type(orig_resp, fake_env)
        assert response.__class__ is SpecialResponse
        strict_eq(response.foo(), 42)
        strict_eq(response.get_data(), b"Hello World!")
        assert response.content_type == "text/html"

    # without env, no arbitrary conversion
    pytest.raises(TypeError, SpecialResponse.force_type, wsgi_application)
Esempio n. 15
0
 def app(request):
     del request  # unused
     return wrappers.BaseResponse(b"an unlikely proto")
Esempio n. 16
0
def test_base_response():
    # unicode
    response = wrappers.BaseResponse(u"öäü")
    strict_eq(response.get_data(), u"öäü".encode("utf-8"))

    # writing
    response = wrappers.Response("foo")
    response.stream.write("bar")
    strict_eq(response.get_data(), b"foobar")

    # set cookie
    response = wrappers.BaseResponse()
    response.set_cookie(
        "foo",
        value="bar",
        max_age=60,
        expires=0,
        path="/blub",
        domain="example.org",
        samesite="Strict",
    )
    strict_eq(
        response.headers.to_wsgi_list(),
        [
            ("Content-Type", "text/plain; charset=utf-8"),
            (
                "Set-Cookie",
                "foo=bar; Domain=example.org; Expires=Thu, "
                "01-Jan-1970 00:00:00 GMT; Max-Age=60; Path=/blub; "
                "SameSite=Strict",
            ),
        ],
    )

    # delete cookie
    response = wrappers.BaseResponse()
    response.delete_cookie("foo")
    strict_eq(
        response.headers.to_wsgi_list(),
        [
            ("Content-Type", "text/plain; charset=utf-8"),
            (
                "Set-Cookie",
                "foo=; Expires=Thu, 01-Jan-1970 00:00:00 GMT; Max-Age=0; Path=/",
            ),
        ],
    )

    # close call forwarding
    closed = []

    @implements_iterator
    class Iterable(object):
        def __next__(self):
            raise StopIteration()

        def __iter__(self):
            return self

        def close(self):
            closed.append(True)

    response = wrappers.BaseResponse(Iterable())
    response.call_on_close(lambda: closed.append(True))
    app_iter, status, headers = run_wsgi_app(response, create_environ(), buffered=True)
    strict_eq(status, "200 OK")
    strict_eq("".join(app_iter), "")
    strict_eq(len(closed), 2)

    # with statement
    del closed[:]
    response = wrappers.BaseResponse(Iterable())
    with response:
        pass
    assert len(closed) == 1
Esempio n. 17
0
def test_base_response():
    # unicode
    response = wrappers.BaseResponse(u'öäü')
    strict_eq(response.get_data(), u'öäü'.encode('utf-8'))

    # writing
    response = wrappers.Response('foo')
    response.stream.write('bar')
    strict_eq(response.get_data(), b'foobar')

    # set cookie
    response = wrappers.BaseResponse()
    response.set_cookie('foo',
                        value='bar',
                        max_age=60,
                        expires=0,
                        path='/blub',
                        domain='example.org')
    strict_eq(response.headers.to_wsgi_list(),
              [('Content-Type', 'text/plain; charset=utf-8'),
               ('Set-Cookie', 'foo=bar; Domain=example.org; Expires=Thu, '
                '01-Jan-1970 00:00:00 GMT; Max-Age=60; Path=/blub')])

    # delete cookie
    response = wrappers.BaseResponse()
    response.delete_cookie('foo')
    strict_eq(
        response.headers.to_wsgi_list(),
        [('Content-Type', 'text/plain; charset=utf-8'),
         ('Set-Cookie',
          'foo=; Expires=Thu, 01-Jan-1970 00:00:00 GMT; Max-Age=0; Path=/')])

    # close call forwarding
    closed = []

    @implements_iterator
    class Iterable(object):
        def __next__(self):
            raise StopIteration()

        def __iter__(self):
            return self

        def close(self):
            closed.append(True)

    response = wrappers.BaseResponse(Iterable())
    response.call_on_close(lambda: closed.append(True))
    app_iter, status, headers = run_wsgi_app(response,
                                             create_environ(),
                                             buffered=True)
    strict_eq(status, '200 OK')
    strict_eq(''.join(app_iter), '')
    strict_eq(len(closed), 2)

    # with statement
    del closed[:]
    response = wrappers.BaseResponse(Iterable())
    with response:
        pass
    assert len(closed) == 1
Esempio n. 18
0
 def app(request):
     del request  # unused
     return wrappers.BaseResponse(b"\x7a\x7ftruncated proto")
Esempio n. 19
0
 def app(request):
     del request  # unused
     return wrappers.BaseResponse(b"very sad", status="502 Bad Gateway")
Esempio n. 20
0
 def app(request):
     result = server_info_pb2.ServerInfoResponse()
     result.compatibility.details = request.headers["User-Agent"]
     return wrappers.BaseResponse(result.SerializeToString())
Esempio n. 21
0
 def my_callable(var1, var2):
     return wrappers.BaseResponse(
         json.dumps(res_dict), content_type="application/json", status=200
     )