@@ -43,11 +43,36 @@ def make_request(cls, path='/', method='GET', user=None, headers=None, data=None
4343 kwargs = {'REQUEST_METHOD' : method , 'PATH_INFO' : path , 'wsgi.input' : StringIO ()}
4444 if qs :
4545 kwargs .update ({'QUERY_STRING' : qs })
46+
47+ # Set content type in initial environ if data is provided
48+ if data and method in ['POST' , 'PUT' , 'PATCH' , 'DELETE' ]:
49+ kwargs .update (
50+ {
51+ 'CONTENT_TYPE' : 'application/json' if is_json else 'application/x-www-form-urlencoded' ,
52+ 'CONTENT_LENGTH' : len (str (data )),
53+ }
54+ )
55+
4656 req = WSGIRequest (kwargs )
4757 else :
4858 kwargs = {'path' : path , 'method' : method }
4959 if qs :
5060 kwargs .update ({'query_string' : qs })
61+
62+ # Set content type in initial scope if data is provided
63+ if data and method in ['POST' , 'PUT' , 'PATCH' , 'DELETE' ]:
64+ headers = kwargs .get ('headers' , [])
65+ headers .extend (
66+ [
67+ [
68+ b'content-type' ,
69+ ('application/json' if is_json else 'application/x-www-form-urlencoded' ).encode (),
70+ ],
71+ [b'content-length' , str (len (str (data ))).encode ()],
72+ ]
73+ )
74+ kwargs ['headers' ] = headers
75+
5176 req = ASGIRequest (kwargs , StringIO ())
5277
5378 req .user = user
@@ -60,12 +85,6 @@ def make_request(cls, path='/', method='GET', user=None, headers=None, data=None
6085 setattr (req , 'GET' , data )
6186 elif method in ['POST' , 'PUT' , 'PATCH' , 'DELETE' ]:
6287 req .read () # trigger RawPostDataException and force DRF to load data from req.POST
63- req .META .update (
64- {
65- 'CONTENT_TYPE' : 'application/json' if is_json else 'application/x-www-form-urlencoded' ,
66- 'CONTENT_LENGTH' : len (str (data )),
67- }
68- )
6988 if is_json :
7089 req ._body = data
7190 req .POST = {}
0 commit comments