Example #1
0
def main(args):
    parser = argparse.ArgumentParser(description='Process uri to create request.')
    parser.add_argument('--version', action='version', version='%(prog)s {0}'.format(version))
    parser.add_argument('uri', help="the uri where the request will be sent")
    group = parser.add_mutually_exclusive_group()

    group.add_argument(REQUEST_OPTION_SHORT, REQUEST_OPTION_LONG, dest=REQUEST_OPTION_DESTINATION,
                       help="(HTTP) Specifies a custom request method to use when communicating with the HTTP server. The specified request will be used instead of the method otherwise used (which defaults to GET). Read the HTTP 1.1 specification for details and explanations. (Common additional HTTP requests include PUT and DELETE, but related technologies like WebDAV offers PROPFIND, COPY, MOVE and more). It can be: {0}".format(
                           ", ".join(HTTP_METHODS)), default=HTTP_GET)
    group.add_argument('-G', '--get', help="Activate the get method", dest=REQUEST_OPTION_DESTINATION,
                       action='store_const', const=HTTP_GET)
    group.add_argument('--put', help="Activate the put method", dest=REQUEST_OPTION_DESTINATION, action='store_const',
                       const=HTTP_PUT)
    group.add_argument('--post', help="Activate the post method", dest=REQUEST_OPTION_DESTINATION, action='store_const',
                       const=HTTP_POST)
    group.add_argument('--del', help="Activate the delete method", dest=REQUEST_OPTION_DESTINATION, action='store_const'
                       , const=HTTP_DELETE)
    group.add_argument('--head', help="Activate the head method", dest=REQUEST_OPTION_DESTINATION, action='store_const',
                       const=HTTP_HEAD)
    group.add_argument('--opts', help="Activate the options method", dest=REQUEST_OPTION_DESTINATION,
                       action='store_const', const=HTTP_OPTIONS)
    group.add_argument('--trace', help="Activate the trace method", dest=REQUEST_OPTION_DESTINATION,
                       action='store_const', const=HTTP_TRACE)
    group.add_argument('--conn', help="Activate the connect method", dest=REQUEST_OPTION_DESTINATION,
                       action='store_const', const=HTTP_CONNECT)
    args = parser.parse_args(args)
    target = uri(args.uri)
    if not validate_method(args.request):
        parser.error("invalid method name: {0}".format(args.request))

    response = request(target, args.request)
    _print(response)
Example #2
0
    def test_append_segment(self):
        google = uri("http://www.google.com")
        google = google.append_segment("segment1")
        self.assertEqual("http://www.google.com/segment1",str(google))

        google = google.append_segment("segment2") #string approach
        self.assertEqual("http://www.google.com/segment1/segment2",str(google))
Example #3
0
 def _segment_test(self, segment):
     google = uri("http://www.google.com")
     try:
         google.append_segment(segment)
     except AssertionError:
         pass
     else:
         self.fail("{0} is an invalid segment then a exception must be raised".format(str(segment)))
Example #4
0
 def test_trim_zero_segment_on_existing_segments(self):
     """
     Test trim 0 segments on a URI that contains a segments portion.
     The expected result is the original instance URI.
     """
     google = uri("http://www.google.com/segment1/segment2")
     trim_result = google.trim_segments(0)
     self.assertEqual(trim_result, google)
Example #5
0
 def test_trim_from_query_on_existing_none_query_and_none_fragment(self):
     """
     Test trim_from_query method on a URI that does not contain query portion and fragment portion.
     The expected result is the original URI.
     """
     google = uri("http://www.google.com")
     trim_result = google.trim_from_query()
     self.assertEqual(trim_result, google)
Example #6
0
 def test_trim_query_on_no_query(self):
     """
     Test trim_query method on a URI that does not contain any query portion.
     The expected result is the original instance URI.
     """
     google = uri("http://www.google.com")
     google_result = google.trim_query()
     self.assertEqual(google_result, google)
Example #7
0
 def test_trim_zero_segment_on_no_segments(self):
     """
     Test trim_segments method on a URI that does not contain any path portion.
     The expected result is the original instance URI.
     """
     google = uri("http://www.google.com")
     google_result = google.trim_segments(0)
     self.assertEqual(google_result, google)
Example #8
0
 def test_trim_from_query_on_existing_query_without_fragment(self):
     """
     Test trim_from_query method on a URI that contains a query portion only.
     The expected result is the original URI.
     """
     google = uri("http://www.google.com?param1=value1&param2=value2")
     trim_result = google.trim_from_query()
     self.assertEqual(trim_result, google)
Example #9
0
 def test_trim_one_segments_on_existing_segments_with_fragment(self):
     """
     Test trim_segments method on a URI that contains a segments portion and a path portion.
     The expected result is a new URI with the first segment and the path.
     """
     google = uri("http://www.google.com/segment1/segment2#fragment")
     trim_result = google.trim_segments(1)
     self.assertNotEqual(trim_result, google)
     self.assertEqual("http://www.google.com/segment1#fragment", str(trim_result))
Example #10
0
 def test_trim_two_segments_on_existing_segments(self):
     """
     Test trim_segments method on a URI that contains a segments portion.
     The expected result a new instance URI with the first segment.
     """
     google = uri("http://www.google.com/segment1/segment2")
     trim_result = google.trim_segments(2)
     self.assertNotEqual(trim_result, google)
     self.assertEqual("http://www.google.com", str(trim_result))
Example #11
0
 def test_trim_path_on_existing_path_with_fragment(self):
     """
     Test trim_path method on a URI that contains a path portion and a path portion.
     The expected result is a new URI with the fragment.
     """
     google = uri("http://www.google.com/segment1/segment2#fragment")
     trim_result = google.trim_path()
     self.assertNotEqual(trim_result, google)
     self.assertEqual("http://www.google.com#fragment", str(trim_result))
Example #12
0
 def test_trim_path_on_existing_path(self):
     """
     Test trim_path method on a URI that contains a path portion.
     The expected result a new instance URI.
     """
     google = uri("http://www.google.com/segment1/segment2")
     trim_result = google.trim_path()
     self.assertNotEqual(trim_result, google)
     self.assertEqual("http://www.google.com", str(trim_result))
Example #13
0
 def test_trim_fragment_on_existing_fragment_with_query(self):
     """
     Test trim_fragment method on a URI that contains a fragment portion and a query portion.
     The expected result is a new URI with the query.
     """
     google = uri("http://www.google.com?param1=value1&param2=value2#fragment")
     trim_result = google.trim_fragment()
     self.assertNotEqual(trim_result, google)
     self.assertEqual("http://www.google.com?param1=value1&param2=value2", str(trim_result))
Example #14
0
 def test_trim_fragment_on_existing_fragment(self):
     """
     Test trim_fragment method on a URI that contains a fragment portion.
     The expected result a new instance URI.
     """
     google = uri("http://www.google.com#fragment")
     trim_result = google.trim_fragment()
     self.assertNotEqual(trim_result, google)
     self.assertEqual("http://www.google.com", str(trim_result))
Example #15
0
 def test_trim_query_on_existing_query_with_path(self):
     """
     Test trim_query method on a URI that contains a query portion and a path portion.
     The expected result is a new URI with the path.
     """
     google = uri("http://www.google.com/segment1/segment2?param1=value1&param2=value2")
     trim_result = google.trim_query()
     self.assertNotEqual(trim_result, google)
     self.assertEqual("http://www.google.com/segment1/segment2", str(trim_result))
Example #16
0
 def test_trim_query_on_existing_query(self):
     """
     Test trim_query method on a URI that contains a query portion.
     The expected result a new instance URI.
     """
     google = uri("http://www.google.com?param1=value1&param2=value2")
     trim_result = google.trim_query()
     self.assertNotEqual(trim_result, google)
     self.assertEqual("http://www.google.com", str(trim_result))
Example #17
0
 def test_trim_one_segments_on_existing_segments_with_query(self):
     """
     Test trim_segments method on a URI that contains a segments portion and a query portion.
     The expected result is a new URI with the first segment and the query.
     """
     google = uri("http://www.google.com/segment1/segment2?param1=value1&param2=value2")
     trim_result = google.trim_segments(1)
     self.assertNotEqual(trim_result, google)
     self.assertEqual("http://www.google.com/segment1?param1=value1&param2=value2", str(trim_result))
Example #18
0
 def test_trim_from_authority_on_none_authority_but_path_and_query_and_fragment(self):
     """
     Test trim_from_authority method on a URI that does not contain a path but a query portion and a fragment portion.
     The expected result is a new URI without the query and the fragment.
     """
     google = uri("http:///segment1/segment2?param1=value1&param2=value2#fragment")
     trim_result = google.trim_from_authority()
     self.assertNotEqual(trim_result, google)
     self.assertEqual("http://", str(trim_result))
Example #19
0
 def test_trim_from_path_on_existing_path_and_query_and_fragment(self):
     """
     Test trim_from_from method on a URI that contains a path, a query portion and a fragment portion.
     The expected result is a new URI without the query and the fragment.
     """
     google = uri("http://www.google.com/segment1/segment2?param1=value1&param2=value2#fragment")
     trim_result = google.trim_from_path()
     self.assertNotEqual(trim_result, google)
     self.assertEqual("http://www.google.com/segment1/segment2", str(trim_result))
Example #20
0
 def test_trim_from_query_on_none_query_and_existing_fragment(self):
     """
     Test trim_from_query method on a URI that does not contain a query portion but a fragment portion.
     The expected result is a new URI without the fragment.
     """
     google = uri("http://www.google.com#fragment")
     trim_result = google.trim_from_query()
     self.assertNotEqual(trim_result, google)
     self.assertEqual("http://www.google.com", str(trim_result))
Example #21
0
    def test_append_query(self):
        google = uri("http://www.google.com")
        google = google.append_query("param1=value1&param2=value2") #string approach
        self.assertEqual("http://www.google.com?param1=value1&param2=value2",str(google))

        google = google.append_query((("param1","value1"),("param2","value2"))) #list key value tuple approach for ordered result
        self.assertEqual("http://www.google.com?param1=value1&param2=value2",str(google))

        google = google.append_query(None) #list key value tuple approach for ordered result
        self.assertEqual("http://www.google.com",str(google))
Example #22
0
 def test_append_fragment(self):
     google = uri("http://www.google.com")
     google = google.append_fragment("fragment")
     self.assertEqual("http://www.google.com#fragment",str(google))
     google = google.append_fragment("fragment2")
     self.assertEqual("http://www.google.com#fragment2",str(google))
     google = google.append_fragment(None)
     self.assertEqual("http://www.google.com",str(google))
     google = google.append_fragment("")
     self.assertEqual("http://www.google.com",str(google))
Example #23
0
 def test_uri_elements(self):
     google = uri("http://romain.gilles:[email protected]:8080/apath/aparam1;aparam2/subpath?q=myquery#afragment")
     self.assertEqual(google.authority, "romain.gilles:[email protected]:8080")
     self.assertEqual(google.username, "romain.gilles")
     self.assertEqual(google.password, "tutu")
     self.assertEqual(google.port, 8080)
     self.assertEqual(google.path, '/apath/aparam1;aparam2/subpath')
     self.assertEqual(google.segments, ('apath','aparam1;aparam2', 'subpath'))
     self.assertEqual(google.query, 'q=myquery')
     self.assertEqual(google.fragment, 'afragment')
Example #24
0
 def test_append_path_on_existing_path(self):
     google = uri("http://www.google.com/existing/path")
     result_uri = google.append_path("/segment1/segment2")
     self.assertIsNotNone(result_uri)
     self.assertEqual("http://www.google.com/segment1/segment2", str(result_uri))
Example #25
0
 def test_append_segments_with_trailing_separator(self):
     google = uri("http://www.google.com")
     google = google.append_segments(("segment1", "segment2", ""))
     self.assertEqual("http://www.google.com/segment1/segment2/",str(google))
Example #26
0
 def test_append_segments_tuple(self):
     google = uri("http://www.google.com")
     google = google.append_segments(("segment1", "segment2"))
     self.assertEqual("http://www.google.com/segment1/segment2",str(google))
Example #27
0
 def test_absolute(self):
     google = uri("http://www.google.com/segment1/segment2?param1=value1&param2=value2#fragment")
     assert_that(google.absolute, is_("/segment1/segment2?param1=value1&param2=value2#fragment"))
Example #28
0
 def test_user_info(self):
     google = uri("http://romain.gilles:[email protected]")
     self.assertEqual(google.authority, "romain.gilles:[email protected]")
     self.assertEqual(google.username, "romain.gilles")
     self.assertEqual(google.password, "tutu")
Example #29
0
 def test_authority(self):
     google = uri("http://www.google.com")
     self.assertEqual(google.authority, "www.google.com")
Example #30
0
 def test_scheme(self):
     google = uri("http://www.google.com")
     self.assertEqual(google.scheme, "http")