def test_buildResponse():
    uri = parse_init_req_line(parse_request(good_request)[0])[0]
    b = getResource(uri)[2]
    h = responseHeaders(getResource(uri)[0], 'Today', getResource(uri)[1])
    response = buildResponse("HTTP/1.1 "+str(getResource(uri)[3])+" OK", h, b)
    assert "HTTP/1.1 200 OK" in response
    assert "image/jpg" in response
    assert "Requested resource is an image. Here is it's binary data vomit:" in response
Beispiel #2
0
def test_resolve_uri():
    uri = parse_request("GET ./ HTTP/1.1\r\nHOST: www.site.com\r\n\r\n<html>stuff</html>")
    assert "text/html" in resolve_uri(uri)
    with pytest.raises(UserWarning):
        resolve_uri("./../../../")
    with pytest.raises(IOError):
        resolve_uri("~")
    with pytest.raises(IOError):
        resolve_uri("./cat.gif")
Beispiel #3
0
def test_parse_request():
    #  parse_request returns the resource requested.
    assert "./webroot/" + "/path/templates/thing.html" == parse_request("GET /path/templates/thing.html HTTP/1.1\r\nHOST: www.site.com\r\n\r\n<html>stuff</html>")
    #  Bad Method
    with pytest.raises(ValueError):
        parse_request("POST /path/thing.html HTTP/1.1\r\nHOST: www.site.com")
    #  Bad request
    with pytest.raises(SyntaxError):
        parse_request("asdfasdF")
    #  Bad HTTP Version
    with pytest.raises(SyntaxError):
        parse_request("GET /path/templates/thing.html HTTP/1.0\r\nHOST: www.aol.com\r\n\r\n<html>stuff</html>")
    #  No Host
    with pytest.raises(SyntaxError):
        parse_request("GET /path/templates/thing.html HTTP/1.0\r\n")
def test_parse_req_for_bad3_URI():
    request = u'DELETE "/webroot/../sample.txt" HTTP/1.1\r\n'
    request.encode('utf-8')
    with pytest.raises(ForbiddenError):
        http_server.parse_request(request)
Beispiel #5
0
def call_parse_request(step):
    try:
        world.cmd = parse_request(world.header)
    except MethodNotAllowedError:
        world.cmd = "405 Error"
Beispiel #6
0
 def call_function_under_test(self, request):
     """call the `parse_request` function"""
     from http_server import parse_request
     return parse_request(request)
Beispiel #7
0
 def call_function_under_test(self, request):
     """call the `parse_request` function"""
     from http_server import parse_request
     return parse_request(request)
    def test_parse_request(self):
        path = "/foo"
        request_head = "GET {} HTTP/1.1".format(path)

        self.assertEqual(path, http_server.parse_request(request_head))
    def test_parse_request_bad_method(self):
        request_head = "POST /foo HTTP/1.1"

        with self.assertRaises(NotImplementedError):
            http_server.parse_request(request_head)
def test_good_request():
    x = parse_request(good_request)[0]
    assert x == "GET webroot/images/Sample_Scene_Balls.jpg HTTP/1.1"
    assert parse_init_req_line(x)[0] == "webroot/images/Sample_Scene_Balls.jpg"
    assert parse_init_req_line(x)[1] == 200
def test_400():
    x = parse_request(bad_http_version)[0]
    assert x == "GET funWithThreads.py HTTP/1.0"
    assert parse_init_req_line(x)[0] == "funWithThreads.py"
    assert parse_init_req_line(x)[1] == 400
def test_405():
    x = parse_request(bad_method_call)[0]
    assert x == "GIT webroot/sample.txt HTTP/1.1"
    assert parse_init_req_line(x)[0] == "webroot/sample.txt"
    assert parse_init_req_line(x)[1] == 405
def test_parse_req_for_URI_GET():
    uri = u'/index.html'
    request = u'GET ' + uri + ' HTTP/1.1\r\nHost: some_host\r\n\r\n'
    request.encode('utf-8')
    assert uri == http_server.parse_request(request)
def test_parse_req_for_URI_invalid_protocol():
    request = u'GET some_URI HTTP/1.0\r\nHost: some_host\r\n\r\n'
    request.encode('utf-8')
    with pytest.raises(VersionNotSupportedError):
        http_server.parse_request(request)
def test_parse_req_for_URI_DELETE():
    request = u'DELETE some_URI HTTP/1.1\r\nHost: some_host\r\n\r\n'
    request.encode('utf-8')
    with pytest.raises(InvalidMethodError):
        http_server.parse_request(request)