def test_get_content(self): """ Stream content. """ def _response_for(method, url, params, headers, data): self.assertThat(method, Equals(b'GET')) self.assertThat( url, MatchesAll( IsInstance(bytes), Equals(b'http://example.com/some_content'))) return (200, {b'Content-Type': b'text/plain'}, b'hello world') resource = StringStubbingResource(_response_for) treq = StubTreq(resource) session = Session({}, treq.request) buf = StringIO() self.assertThat( session.get_content(b'http://example.com/some_content', buf.write), succeeded( Equals(b'text/plain'))) self.assertThat( buf.getvalue(), Equals(b'hello world'))
def test_perform_action(self): """ Perform an action within a session. """ payload = {u'links': {u'result': u'https://example.com/result'}} action = {u'action': u'some_action', u'parameters': {u'foo': 42}} def _response_for(method, url, params, headers, data): self.assertThat(method, Equals(b'POST')) self.assertThat( url, MatchesAll( IsInstance(bytes), Equals(b'http://example.com/perform'))) self.assertThat( json.loads(data), Equals(action)) return (200, {b'Content-Type': b'application/json'}, json.dumps(payload)) resource = StringStubbingResource(_response_for) treq = StubTreq(resource) session = Session({u'perform': u'http://example.com/perform'}, treq.request) self.assertThat( session.perform_action((action, lambda x: x)), succeeded(Equals(payload)))
def test_delete(self): """ Delete a session. """ def _response_for(method, url, params, headers, data): self.assertThat(method, Equals(b'DELETE')) self.assertThat( url, MatchesAll( IsInstance(bytes), Equals(b'http://example.com/session'))) return 200, {}, b'' resource = StringStubbingResource(_response_for) treq = StubTreq(resource) session = Session({u'self': u'http://example.com/session'}, treq.request) self.assertThat( session.delete(), succeeded(Equals(b'')))
def test_store_content(self): """ Store content in a Documint session. """ def _response_for(method, url, params, headers, data): self.assertThat(method, Equals(b'POST')) self.assertThat( headers, ContainsDict({ b'Accept': Equals([b'application/json']), b'Content-Type': Equals([b'text/plain'])})) self.assertThat( url, MatchesAll( IsInstance(bytes), Equals(b'http://example.com/store'))) self.assertThat( data, MatchesAll( IsInstance(bytes), Equals(b'hello world'))) return (200, {b'Content-Type': b'application/json'}, json.dumps( {u'links': {u'self': u'http://example.com/stored_object'}})) resource = StringStubbingResource(_response_for) treq = StubTreq(resource) session = Session({u'store-content': u'http://example.com/store'}, treq.request) # XXX: This is not a real file object because `StubTreq` doesn't # implement support for that. fileobj = b'hello world' self.assertThat( session.store_content(fileobj, b'text/plain'), succeeded( Equals(u'http://example.com/stored_object')))