simple_http_requests_test.py
Go to the documentation of this file.
1 #! /usr/bin/env python
2 
3 import sys
4 if sys.version_info > (3,):
5  import http.client as httplib
6 else:
7  import httplib
8 import rospy
9 import unittest
10 import time
11 
12 class TestSimpleHttpRequests(unittest.TestCase):
13  def setUp(self):
14  self.conn = httplib.HTTPConnection("localhost:9849")
15 
16  def test_ok(self):
17  self.conn.request("GET", "/response/ok")
18  response = self.conn.getresponse()
19  self.assertEqual(200, response.status)
20 
21  def test_created(self):
22  self.conn.request("GET", "/response/created")
23  response = self.conn.getresponse()
24  self.assertEqual(201, response.status)
25 
26  def test_accepted(self):
27  self.conn.request("GET", "/response/accepted")
28  response = self.conn.getresponse()
29  self.assertEqual(202, response.status)
30 
31  def test_forbidden(self):
32  self.conn.request("GET", "/response/forbidden")
33  response = self.conn.getresponse()
34  self.assertEqual(403, response.status)
35 
36  def test_not_found(self):
37  self.conn.request("GET", "/response/not_found")
38  response = self.conn.getresponse()
39  self.assertEqual(404, response.status)
40 
42  self.conn.request("GET", "/response/internal_server_error")
43  response = self.conn.getresponse()
44  self.assertEqual(500, response.status)
45 
47  self.conn.request("GET", "/some_random_url12345")
48  response = self.conn.getresponse()
49  self.assertEqual(404, response.status)
50 
52  self.conn.request("GET", "/a_static_response")
53  response = self.conn.getresponse()
54  self.assertEqual(200, response.status)
55  self.assertEqual(b"A RESPONSE", response.read())
56 
57  def test_http_echo1(self):
58  test_content = b"hello HELLO"*1000 # make sure to exceed MTU
59  self.conn.request("GET", "/http_body_echo", test_content)
60  response = self.conn.getresponse()
61  self.assertEqual(200, response.status)
62  self.assertEqual(test_content, response.read())
63 
64  def test_http_echo2(self):
65  test_content = b"THIS is A test"*1000 # make sure to exceed MTU
66  self.conn.request("POST", "/http_body_echo", test_content)
67  response = self.conn.getresponse()
68  self.assertEqual(200, response.status)
69  self.assertEqual(test_content, response.read())
70 
72  self.conn.request("GET", "/http_path_echo/this_is_a_test")
73  response = self.conn.getresponse()
74  self.assertEqual(200, response.status)
75  self.assertEqual(b"/http_path_echo/this_is_a_test", response.read())
76 
78  self.conn.request("GET", "/http_query_echo?hello=1&b=test&c=10")
79  response = self.conn.getresponse()
80  self.assertEqual(200, response.status)
81  self.assertEqual(b"b=test\nc=10\nhello=1\n", response.read())
82 
83  def test_file(self):
84  self.conn.request("GET", "/test_file")
85  response = self.conn.getresponse()
86  self.assertEqual(200, response.status)
87  self.assertEqual(b"<html></html>\n", response.read())
88 
90  self.conn.request("GET", "/test_files/test_dir/test_file.txt")
91  response = self.conn.getresponse()
92  self.assertEqual(200, response.status)
93  self.assertEqual(b"test\n", response.read())
94 
96  self.conn.request("GET", "/test_files/test_dir/")
97  response = self.conn.getresponse()
98  self.assertEqual(403, response.status)
99 
101  self.conn.request("GET", "/test_files/test_dir")
102  response = self.conn.getresponse()
103  self.assertEqual(403, response.status)
104 
106  self.conn.request("GET", "/test_files_with_dir/test_dir/")
107  response = self.conn.getresponse()
108  self.assertEqual(200, response.status)
109 
110 if __name__ == '__main__':
111  time.sleep(1) # ensure server is up
112 
113  import rostest
114  rospy.init_node('simple_http_requests_test')
115  rostest.rosrun('async_web_server_cpp', 'simple_http_requests', TestSimpleHttpRequests)


async_web_server_cpp
Author(s): Mitchell Wills , Russel Toris
autogenerated on Mon Feb 28 2022 21:54:08