예제 #1
0
    def test_body_kwargs_bad_content_type(self):
        """make sure a form upload content type with json body fails correctly"""
        r = Request()
        r.body = "foo=bar&che=baz&foo=che"
        r.headers = {'content-type': 'application/json'}
        with self.assertRaises(ValueError):
            br = r.body_kwargs

        r.body = '{"foo": ["bar", "che"], "che": "baz"}'
        r.headers = {'content-type': "application/x-www-form-urlencoded"}

        with self.assertRaises(ValueError):
            br = r.body_kwargs
예제 #2
0
    def test_body_kwargs_bad_content_type(self):
        """make sure a form upload content type with json body fails correctly"""
        r = Request()
        r.body = "foo=bar&che=baz&foo=che"
        r.headers = {'content-type': 'application/json'}
        with self.assertRaises(ValueError):
            br = r.body_kwargs

        r.body = '{"foo": ["bar", "che"], "che": "baz"}'
        r.headers = {'content-type': "application/x-www-form-urlencoded"}

        with self.assertRaises(ValueError):
            br = r.body_kwargs
예제 #3
0
    def test_body_kwargs(self):
        #body = "foo=bar&che=baz&foo=che"
        #body_kwargs = {'foo': ['bar', 'che'], 'che': 'baz'}
        #body_json = '{"foo": ["bar", "che"], "che": "baz"}'
        cts = {
            "application/x-www-form-urlencoded": (
                "foo=bar&che=baz&foo=che",
                {'foo': ['bar', 'che'], 'che': 'baz'}
            ),
#             'application/json': (
#                 '{"foo": ["bar", "che"], "che": "baz"}',
#                 {'foo': ['bar', 'che'], 'che': 'baz'}
#             ),
        }

        for ct, bodies in cts.items():
            ct_body, ct_body_kwargs = bodies

            r = Request()
            r.body = ct_body
            r.set_header('content-type', ct)
            self.assertTrue(isinstance(r.body_kwargs, dict))
            self.assertEqual(r.body_kwargs, ct_body_kwargs)

            r = Request()
            r.set_header('content-type', ct)
            self.assertEqual(r.body_kwargs, {})
            self.assertEqual(r.body, None)

            r = Request()
            r.set_header('content-type', ct)
            r.body_kwargs = ct_body_kwargs
            self.assertEqual(r._parse_query_str(r.body), r._parse_query_str(ct_body))
예제 #4
0
    def test_body_kwargs(self):
        #body = "foo=bar&che=baz&foo=che"
        #body_kwargs = {'foo': ['bar', 'che'], 'che': 'baz'}
        #body_json = '{"foo": ["bar", "che"], "che": "baz"}'
        cts = {
            "application/x-www-form-urlencoded": ("foo=bar&che=baz&foo=che", {
                'foo': ['bar', 'che'],
                'che': 'baz'
            }),
            #             'application/json': (
            #                 '{"foo": ["bar", "che"], "che": "baz"}',
            #                 {'foo': ['bar', 'che'], 'che': 'baz'}
            #             ),
        }

        for ct, bodies in cts.iteritems():
            ct_body, ct_body_kwargs = bodies

            r = Request()
            r.body = ct_body
            r.set_header('content-type', ct)
            self.assertTrue(isinstance(r.body_kwargs, dict))
            self.assertEqual(r.body_kwargs, ct_body_kwargs)

            r = Request()
            r.set_header('content-type', ct)
            self.assertEqual(r.body_kwargs, {})
            self.assertEqual(r.body, None)

            r = Request()
            r.set_header('content-type', ct)
            r.body_kwargs = ct_body_kwargs
            self.assertEqual(r._parse_query_str(r.body),
                             r._parse_query_str(ct_body))
예제 #5
0
    def test_body(self):
        # simulate a problem I had with a request with curl
        r = Request()
        r.method = 'GET'
        r.body = ""
        r.set_headers({
            'PATTERN': "/",
            'x-forwarded-for': "127.0.0.1",
            'URI': "/",
            'accept': "*/*",
            'user-agent': "curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8y zlib/1.2.5",
            'host': "localhost",
            'VERSION': "HTTP/1.1",
            'PATH': "/",
            'METHOD': "GET",
            'authorization': "Basic SOME_HASH_THAT_DOES_NOT_MATTER="
        })
        self.assertEqual("", r.body)

        r = Request()
        r.method = 'POST'

        r.set_header('content-type', "application/x-www-form-urlencoded")
        r.body = "foo=bar&che=baz&foo=che"
        body_r = {'foo': ['bar', 'che'], 'che': 'baz'}
        self.assertEqual(body_r, r.body_kwargs)


        r.body = None
        #del(r._body_kwargs)
        body_r = {}
        self.assertEqual(body_r, r.body_kwargs)

        r.set_header('content-type', "application/json")
        r.body = '{"person":{"name":"bob"}}'
        #del(r._body_kwargs)
        body_r = {'person': {"name":"bob"}}
        self.assertEqual(body_r, r.body_kwargs)

        r.body = ''
        #del(r._body_kwargs)
        body_r = ''
        self.assertEqual(body_r, r.body)

        r.headers = {}
        body = '{"person":{"name":"bob"}}'
        r.body = body
        self.assertEqual(body, r.body)

        r.method = 'GET'
        r.set_header('content-type', "application/json")
        r.body = None
        self.assertEqual(None, r.body)
예제 #6
0
    def test_body(self):
        # simulate a problem I had with a request with curl
        r = Request()
        r.method = 'GET'
        r.body = ""
        r.set_headers({
            'PATTERN': "/",
            'x-forwarded-for': "127.0.0.1",
            'URI': "/",
            'accept': "*/*",
            'user-agent':
            "curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8y zlib/1.2.5",
            'host': "localhost",
            'VERSION': "HTTP/1.1",
            'PATH': "/",
            'METHOD': "GET",
            'authorization': "Basic SOME_HASH_THAT_DOES_NOT_MATTER="
        })
        self.assertEqual("", r.body)

        r = Request()
        r.method = 'POST'

        r.set_header('content-type', "application/x-www-form-urlencoded")
        r.body = "foo=bar&che=baz&foo=che"
        body_r = {'foo': ['bar', 'che'], 'che': 'baz'}
        self.assertEqual(body_r, r.body_kwargs)

        r.body = None
        #del(r._body_kwargs)
        body_r = {}
        self.assertEqual(body_r, r.body_kwargs)

        r.set_header('content-type', "application/json")
        r.body = '{"person":{"name":"bob"}}'
        #del(r._body_kwargs)
        body_r = {'person': {"name": "bob"}}
        self.assertEqual(body_r, r.body_kwargs)

        r.body = ''
        #del(r._body_kwargs)
        body_r = ''
        self.assertEqual(body_r, r.body)

        r.headers = {}
        body = '{"person":{"name":"bob"}}'
        r.body = body
        self.assertEqual(body, r.body)

        r.method = 'GET'
        r.set_header('content-type', "application/json")
        r.body = None
        self.assertEqual(None, r.body)