def test_request(self): """ Makes a POST request and decodes JSON responses. """ def _response_for(method, url, params, headers, data): self.assertThat(method, Equals(u'POST')) self.assertThat(url, Equals(b'http://example.com/post_json')) self.assertThat( headers, ContainsDict( {b'Accept': Equals([b'application/json']), b'Content-Type': Equals([b'application/json'])})) return 200, {}, json.dumps({u'arst': u'arst'}) resource = StringStubbingResource(_response_for) treq = StubTreq(resource) self.assertThat( post_json(treq.request, b'http://example.com/post_json'), succeeded( Equals({u'arst': u'arst'})))
def test_own_content_type(self): """ If an existing ``Content-Type`` header exists it is used instead of ``application/json``. """ def _response_for(method, url, params, headers, data): self.assertThat(method, Equals(u'POST')) self.assertThat(url, Equals(b'http://example.com/post_json')) self.assertThat( headers, ContainsDict( {b'Accept': Equals([b'application/json']), b'Content-Type': Equals([b'text/plain'])})) return 200, {}, json.dumps({u'arst': u'arst'}) resource = StringStubbingResource(_response_for) treq = StubTreq(resource) self.assertThat( post_json(treq.request, b'http://example.com/post_json', headers={b'Content-Type': b'text/plain'}), succeeded( Equals({u'arst': u'arst'})))