PACT PACT (pactman): Pact не обрабатывает 'application / x-www-form-urlencoded'? - PullRequest
1 голос
/ 20 марта 2019

Я недавно выполнял CDC (потребительские контракты) и столкнулся со следующей проблемой:

У меня есть потребительский тест:

@pytest.fixture()
def consumer(session, pact):
    return Service(session, pact.uri)

def test_get_user_code(pact, consumer):
    expected = {"device_code": "xxxxx",
                "user_code": "12345",
                }
    (pact
     .given('everything is ideal')
     .upon_receiving('a request for device and user codes')
     .with_request(
         method='POST',
         path="/some_path/path/authorization",
         headers={'Content-Type': 'application/x-www-form-urlencoded'},
         query={"client_id": ["test1"],
                "secret": ["test1-secret-key"],
                "scope": ['openid', 'profile', 'phone', 'offline_access']
                }
     )
     .will_respond_with(200, body=Like(expected)))

     with pact:
        r = consumer.get_device_n_user_codes('test1', 'test1-secret-key',
                                         'openid profile phone offline_access',
                                          imposter=pact.uri + '/')

Запрос в Fiddler / Burpвыглядит так:

POST /some_path/path/authorization HTTP/1.1
Host: some_url.com
User-Agent: python-requests/2.18.4
Accept-Encoding: gzip, deflate
Accept: */*
Connection: close
Content-Type: application/x-www-form-urlencoded
Content-Length: 104

client_id=test1&secret=test1-secret- 
key&scope=openid+profile+phone+offline_access

И это работает нормально против реального провайдера (я получаю ожидаемый ответ).Однако, когда я использую макетный сервер Pact / Pactman, я получаю следующую ошибку:

>                                                imposter=pact.uri + '/')

test_get_usercodes_n_access_token.py:49: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
..\..\..\model\authorization.py:78: in get_device_n_user_codes
imposter=imposter)
..\..\..\model\http.py:71: in parametrized_post
verify=False)
..\..\..\venv\lib\site-packages\requests\api.py:112: in post
return request('post', url, data=data, json=json, **kwargs)
..\..\..\venv\lib\site-packages\requests\api.py:58: in request
return session.request(method=method, url=url, **kwargs)
..\..\..\venv\lib\site-packages\requests\sessions.py:508: in request
resp = self.send(prep, **send_kwargs)
..\..\..\venv\lib\site-packages\requests\sessions.py:618: in send
r = adapter.send(request, **kwargs)
..\..\..\venv\lib\site-packages\requests\adapters.py:440: in send
timeout=timeout
..\..\..\venv\lib\site-packages\pactman\mock\mock_urlopen.py:32: in urlopen
return self.mocks[self.port](method, url, body=body, data=data, 
headers=headers)
..\..\..\venv\lib\site-packages\pactman\mock\mock_urlopen.py:82: in __call__
return self.validate_request(method)
..\..\..\venv\lib\site-packages\pactman\mock\pact_request_handler.py:51: in 
validate_request
body = json.loads(self.body)
C:\Users\user\AppData\Local\Programs\Python\Python36- 
32\lib\json\__init__.py:354: in loads
return _default_decoder.decode(s)
C:\Users\user\AppData\Local\Programs\Python\Python36- 
32\lib\json\decoder.py:339: in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <json.decoder.JSONDecoder object at 0x0421E0D0>
s = 'client_id=test1&secret=test1-secret-key&scope=openid+profile+phone+offline_access'
idx = 0

   def raw_decode(self, s, idx=0):
       """Decode a JSON document from ``s`` (a ``str`` beginning with
        a JSON document) and return a 2-tuple of the Python
        representation and the index in ``s`` where the document ended.

        This can be used to decode a JSON document from a string that may
        have extraneous data at the end.

        """
       try:
           obj, end = self.scan_once(s, idx)
       except StopIteration as err:
 >          raise JSONDecodeError("Expecting value", s, err.value) from None
 E          json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
 C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\json\decoder.py:357: JSONDecodeError
 ---------------------------- Captured stdout setup ----------------------------
...