def test_http_get_method_google(self): httpc_connection = httpc.HttpConnection("www.google.com", 80) httpc_connection.request('GET', '', agent='UnitTests') httpc_response = httpc_connection.getresponse() httpc_connection.close() self.assertEquals(httpc_response.status_code, 302)
def send_http(request): if not request['url'].startswith('http'): request['url'] = '%s%s' % ('http://', request['url']) url_parse = urlparse(request['url']) http_connection = httpc.HttpConnection(url_parse.netloc, 80, output=request['verbose']) path = url_parse.path if request['type'] == 'GET': if len(url_parse.query) > 0: path = '{}?{}'.format(url_parse.path, url_parse.query) request['header']['Agent'] = 'http-client' if request['data'] is None: http_connection.request(request['type'], path, headers=request['header']) else: http_connection.request(request['type'], path, request['data']['value'], headers=request['header']) result = http_connection.getresponse() if request['redirect'] and result.status_code == 302: if 'Location' not in result.headers: print('No redirect location found in response header') print(result.body) return location = result.headers['Location'] if request['verbose']: print('Redirect enabled, redirecting to {0}'.format(location)) request['url'] = location send_http(request) else: print(result.body) return http_connection
def test_http_get_method_without_parameters_img(self): httpc_connection = httpc.HttpConnection("www.httpbin.org", 80) httpc_connection.request('GET', '/image/png') httpc_response = httpc_connection.getresponse() httpc_connection.close() conn = http.client.HTTPSConnection("www.httpbin.org") conn.request("GET", "/image/png") http_response = conn.getresponse() http_response_body = http_response.read() conn.close() self.assertEqual(httpc_response.body, http_response_body) self.assertEquals(httpc_response.status_code, http_response.status)
def test_http_get_method_with_parameters(self): httpc_connection = httpc.HttpConnection("www.httpbin.org", 80) httpc_connection.request('GET', '/get?course=networking&assignment=1') httpc_response = httpc_connection.getresponse() httpc_response_args = json.loads(httpc_response.body)['args'] httpc_connection.close() conn = http.client.HTTPSConnection("www.httpbin.org") conn.request("GET", "/get?course=networking&assignment=1") http_response = conn.getresponse() http_response_body = http_response.read().decode("utf-8") http_response_args = json.loads(http_response_body)['args'] conn.close() self.assertEqual(httpc_response_args, http_response_args) self.assertEquals(httpc_response.status_code, http_response.status)
def test_http_get_method_without_parameters(self): # Test our library httpc_connection = httpc.HttpConnection("www.httpbin.org", 80) httpc_connection.request('GET', '/status/418', agent='UnitTests') httpc_response = httpc_connection.getresponse() httpc_connection.close() # Test against pythons official HTTP library conn = http.client.HTTPSConnection("www.httpbin.org") conn.request("GET", "/status/418") http_response = conn.getresponse() http_response_body = http_response.read().decode("utf-8") conn.close() self.assertEqual(httpc_response.body, http_response_body) self.assertEquals(httpc_response.status_code, http_response.status)
def test_http_get_method_concordia(self): # Test our library httpc_connection = httpc.HttpConnection("www.yayonay.me", 80) httpc_connection.request('GET', '') httpc_response = httpc_connection.getresponse() httpc_connection.close() # Test against pythons official HTTP library conn = http.client.HTTPConnection("www.yayonay.me", 80) conn.request("GET", "") http_response = conn.getresponse() http_response_body = http_response.read().decode("utf-8") conn.close() self.assertEqual(httpc_response.body, http_response_body) self.assertEquals(httpc_response.status_code, http_response.status)
def test_http_post_method_without_parameters(self): body = "comments=&custemail=harley.1011%40gmail.com&custname=Harley&delivery=&size=small&topping=bacon" httpc_connection = httpc.HttpConnection("www.httpbin.org", 80) headers = {} headers['Content-Type'] = 'application/x-www-form-urlencoded' headers['Content-Length'] = len(body) headers['Accept-Encoding'] = 'identity' httpc_connection.request('POST', '/post', body, headers) httpc_response = httpc_connection.getresponse() httpc_connection.close() conn = http.client.HTTPSConnection("www.httpbin.org") conn.request("POST", "/post", body, headers) http_response = conn.getresponse() http_response_body = http_response.read().decode('utf-8') conn.close() self.assertEqual( json.loads(httpc_response.body)['form'], json.loads(http_response_body)['form']) self.assertEqual(httpc_response.status_code, 200)