def test_recording_calls(port): ("HTTPretty should be able to record calls") # Given a destination path: destination = FIXTURE_FILE("recording-1.json") # When I record some calls with HTTPretty.record(destination): requests.get(server_url("/foobar?name=Gabriel&age=25", port)) requests.post(server_url("/foobar", port), data=json.dumps({'test': '123'}), headers={"Test": "foobar"}) # Then the destination path should exist os.path.exists(destination).should.be.true # And the contents should be json raw = open(destination).read() json.loads.when.called_with(raw).should_not.throw(ValueError) # And the contents should be expected data = json.loads(raw) data.should.be.a(list) data.should.have.length_of(2) # And the responses should have the expected keys response = data[0] response.should.have.key("request").being.length_of(5) response.should.have.key("response").being.length_of(3) response['request'].should.have.key("method").being.equal("GET") response['request'].should.have.key("headers").being.a(dict) response['request'].should.have.key("querystring").being.equal({ "age": [ "25" ], "name": [ "Gabriel" ] }) response['response'].should.have.key("status").being.equal(200) response['response'].should.have.key("body").being.an(text_type) response['response'].should.have.key("headers").being.a(dict) response['response']["headers"].should.have.key("Server").being.equal("TornadoServer/" + tornado_version) # And When I playback the previously recorded calls with HTTPretty.playback(destination): # And make the expected requests response1 = requests.get(server_url("/foobar?name=Gabriel&age=25", port)) response2 = requests.post( server_url("/foobar", port), data=json.dumps({'test': '123'}), headers={"Test": "foobar"}, ) # Then the responses should be the expected response1.json().should.equal({"foobar": {"age": "25", "name": "Gabriel"}}) response2.json()["foobar"].should.equal({}) response2.json()["req_body"].should.equal(json.dumps({"test": "123"})) response2.json()["req_headers"].should.have.key("Test") response2.json()["req_headers"]["Test"].should.equal("foobar")
def test_httpretty_should_load_json_and_playback_contents(): u"HTTPretty should take a simple JSON serialized request object and play it back." with HTTPretty.playback(os.path.join(DIR, 'recordings/hello.world.json')): response = requests.post('http://httpbin.org/post', {'hello': 'world!'}) assert that(response.text).contains('origin') assert that(HTTPretty.last_request.method).equals('POST') assert that(HTTPretty.last_request.path).equals('/post')
def test_playing_calls(): ("HTTPretty should be able to record calls") # Given a destination path: destination = FIXTURE_FILE("playback-1.json") # When I playback some previously recorded calls with HTTPretty.playback(destination): # And make the expected requests response1 = requests.get(server_url("/foobar?name=Gabriel&age=25")) response2 = requests.post(server_url("/foobar"), data=json.dumps({'test': '123'})) # Then the responses should be the expected response1.json().should.equal({"foobar": {"age": "25", "name": "Gabriel"}}) response2.json().should.equal({"foobar": {}})