def test_normal_response_frame(): response = HTTParser.parse( "HTTP/1.1 200 OK\r\n" "Date: Fri, 08 Jul 2011 00:59:41 GMT\r\n" "Server: Apache/2.2.4 (Unix) PHP/5.2.0\r\n" "Content-Type: text/html\r\n" "Content-Length: 37\r\n" "\r\n" "<html>" "<body>" "Hello World" "</body>" "</html>" ) # check status code is 200 assert response.status.code == 200 # check verb is OK assert response.status.verb == 'OK' # check http version is 1.1 assert response.status.protocol_version == '1.1' # check content type header assert response.headers['content-type'].value == 'text/html' # check http body assert response.content == "<html><body>Hello World</body></html>"
def test_header_malformed_form(): response = HTTParser.parse( "HTTP1.0 200 OK\r\n" "Date Fri, 08 Jul 2011 00:59:41 GMT\r\n" "\r\n" "<html>" "<body>" "Hello World" "</body>" "</html>" ) assert response is None
def test_normal_multiline_header_request_frame(): request = HTTParser.parse( "GET / HTTP/1.1\r\n" "Date: Fri, 08 Jul 2011 00:59:41 GMT\r\n" "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) \r\n" " AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2462.0 Safari/537.36\r\n" "\r\n" ) # check user-agent header assert request.headers['user-agent'].value == "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 " "(KHTML, like Gecko) Chrome/46.0.2462.0 Safari/537.36"
def test_no_verb_form(): response = HTTParser.parse( "HTTP/1.1 200\r\n" "Date: Fri, 08 Jul 2011 00:59:41 GMT\r\n" "Server: Apache/2.2.4 (Unix) PHP/5.2.0\r\n" "Content-Type: text/html\r\n" "Content-Length: 37\r\n" "\r\n" "<html>" "<body>" "Hello World" "</body>" "</html>" ) assert response is None
def test_normal_no_value_header_response_frame(): request = HTTParser.parse( "GET / HTTP/1.1\r\n" "Date: \r\n" "Server: \r\n" "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) \r\n" " AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2462.0 Safari/537.36\r\n" "\r\n" ) assert len(request.headers['date'].value) == 0 # check user-agent header assert request.headers['user-agent'].value == "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 " "(KHTML, like Gecko) Chrome/46.0.2462.0 Safari/537.36"
def test_no_header_frame(): request = HTTParser.parse( "GET / HTTP/1.1\r\n" "\r\n" ) # check method assert request.status.method == 'GET' # check verb is OK assert request.status.path == '/' # check http version is 1.1 assert request.status.protocol_version == '1.1' # check header is empty assert request.headers == {} # Empty headers
def test_normal_multiline_header_response_frame(): response = HTTParser.parse( "HTTP/1.1 200 OK\r\n" "Date: Fri, 08 Jul 2011 00:59:41 GMT \r\n" "Server: Apache/2.2.4 (Unix) \r\n" " PHP/5.2.0\r\n" "Content-Type: text/html\r\n" "Content-Length: 37\r\n" "\r\n" "<html>" "<body>" "Hello World" "</body>" "</html>" ) # check server header assert response.headers['server'].value == 'Apache/2.2.4 (Unix) PHP/5.2.0'
def test_normal_request_frame(): request = HTTParser.parse( "GET / HTTP/1.1\r\n" "Date: Fri, 08 Jul 2011 00:59:41 GMT\r\n" "User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 (KHTML, like Gecko) " "Chrome/46.0.2462.0 Safari/537.36\r\n" "\r\n" ) # check method assert request.status.method == 'GET' # check verb is OK assert request.status.path == '/' # check http version is 1.1 assert request.status.protocol_version == '1.1' correct_user_agent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_4) AppleWebKit/537.36 " "(KHTML, like Gecko) Chrome/46.0.2462.0 Safari/537.36" # check content type header assert request.headers['user-agent'].value == correct_user_agent
def test_status_line_malformed_request_form(): request = HTTParser.parse( "GET 11/1/1/1/1/1/1/1" ) assert request is None