Example #1
0
    def test_errors(self):
        ''' Tests that errors are raised for status codes
        Errors should be raised for URL error responses (4xx and 5xx)
        '''
        r = ResponseTest(400, {}, "Bad Request")
        with self.assertRaises(RequestError) as err:
            Response(r, self.app)

        self.assertEqual(str(err.exception), "Bad Request")

        r = ResponseTest(500, {}, "Server Error")
        with self.assertRaises(ServerError) as err:
            Response(r, self.app)

        self.assertEqual(str(err.exception), "Server Error")
Example #2
0
    def test_URLs(self):
        ''' Test that the URL special datatype handler works
        Tests the special handler for URL datatypes gets identified and used
        '''
        params = {"url": "/test"}
        r = ResponseTest(200, {"Content-Type": "application/json"},
                         json.dumps(params))
        response = Response(r, self.app)

        self.assertEqual(response.data["url"], "http://nope/test")
Example #3
0
    def test_date(self):
        ''' Tests that datetime strings are handled as datetimes
        Tests the various formats for datetime strings and makes sure they get
        converted into the proper structure.
        '''
        params = {"date": "12/24/99"}
        r = ResponseTest(200, {"Content-Type": "application/json"},
                         json.dumps(params))
        response = Response(r, self.app)

        self.assertEqual(response.data["date"].strftime("%Y%m%d"), "19991224")
Example #4
0
    def test_json(self):
        ''' Tests that the `Content-Type` for json is identified and parsed
        This tests the __json_handler for mimetypes in `Response`
        '''
        r = ResponseTest(200, {"Content-Type": "application/json"},
                         "{\"foo\":\"bar\"}")
        response = Response(r, self.app)

        self.assertTrue(isinstance(response.data, dict))
        self.assertTrue("foo" in response.data)
        self.assertEqual(response.data["foo"], "bar")
Example #5
0
    def test_nonroot_mismatchURLs(self):
        ''' Tests that URLs of a non root base URL handle mismatches
        This is when the passed in URL is for a root folder that is not the
        same as the base subfolder of the application.
        '''
        self.app = Restler("http://nope/test/")
        params = {"url": "/derp"}
        r = ResponseTest(200, {"Content-Type": "application/json"},
                         json.dumps(params))
        response = Response(r, self.app)

        self.assertEqual(response.data["url"], "/derp")
Example #6
0
    def test_fullURLs(self):
        ''' Test that full URLs are handled
        Tests the handler for full URLs (ones including the host) are
        caught and properly converted.
        '''
        params = {"url": "http://nope/test2"}
        r = ResponseTest(200, {"Content-Type": "application/json"},
                         json.dumps(params))
        response = Response(r, self.app)

        self.assertTrue(isinstance(response.data["url"], Route))
        self.assertEqual(response.data["url"], "http://nope/test2")
Example #7
0
    def test_encoded(self):
        ''' Tests that the `Content-Type` for url encoded works
        This tests the __form_handler for mimetypes in `Response`
        '''
        r = ResponseTest(200,
                         {"Content-Type": "application/x-www-form-urlencoded"},
                         "foo=bar&bar=baz")
        response = Response(r, self.app)

        self.assertTrue(isinstance(response.data, dict))
        self.assertTrue("foo" in response.data)
        self.assertEqual(response.data["foo"], "bar")
Example #8
0
    def test_nonroot_URLs(self):
        ''' Test that URLs of a non root base URL are handled properly
        This is a test for when an app uses a path in the base URL (i.e.
        something like `http://twitter.com/api/`) so URLs that get converted
        take into account this additional base folder
        '''
        self.app = Restler("http://nope/test/")
        params = {"url": "/test"}
        r = ResponseTest(200, {"Content-Type": "application/json"},
                         json.dumps(params))
        response = Response(r, self.app)

        self.assertEqual(response.data["url"], "http://nope/test")
        # Bug, url in above doesn't have trailing slash, messed up with the natural
        # trailing slash
        self.app = Restler("http://nope/test/")
        params = {"url": "/test/"}
        r = ResponseTest(200, {"Content-Type": "application/json"},
                         json.dumps(params))
        response = Response(r, self.app)

        self.assertEqual(response.data["url"], "http://nope/test")
Example #9
0
    def test_links(self):
        ''' Tests the parsing of the Link header
        If a Link header is present, the response should have a `link` property
        which exposes easy access to the attributes of the header.
        '''
        r = ResponseTest(
            200, {
                "Link":
                "</next>; rel=\"next\", " + "</args?test=1>; rel=\"test\", " +
                "<http://nope/full>; rel=\"full\""
            }, "")
        response = Response(r, self.app)

        self.assertEquals(str(response.links.next), "http://nope/next")
        self.assertDictEqual(response.links.test._default_params,
                             {"test": "1"})
        self.assertEquals(str(response.links.full), "http://nope/full")

        self.assertEqual(response.links.none, None)