|
3 | 3 | describe Cortex::Request do |
4 | 4 | let(:client) { Cortex::Client.new(access_token: '123') } |
5 | 5 |
|
6 | | - context 'with a cortex error response' do |
7 | | - it 'should return the error object' do |
8 | | - body = {'error' => 'Validation error or something'} |
9 | | - response = OpenStruct.new(status: 422, body: body, headers: { :whatever => "Whatever"}) |
10 | | - expect(client.parse_response(response).to_h).to eq({ :body => body, :headers => {status: 422}}) |
| 6 | + context 'Request Methods' do |
| 7 | + let(:response) { OpenStruct.new({body: "Body", headers: {}, status: 200} ) } |
| 8 | + let(:model) { { title: 'Test' } } |
| 9 | + |
| 10 | + before(:each) do |
| 11 | + @connection = stub |
| 12 | + Cortex::Client.any_instance.stubs(:connection).returns(@connection) |
| 13 | + Cortex::Result.any_instance.stubs(:new).with(response).returns(true) |
11 | 14 | end |
12 | | - end |
13 | 15 |
|
14 | | - context 'with a non-cortex error response' do |
15 | | - it 'should return a wrapped response' do |
16 | | - body = 'Catastrophic error' |
17 | | - response = OpenStruct.new(status: 500, body: body, headers: { whatever: "Whatever", status: 500 } ) |
18 | | - parsed = client.parse_response(response) |
19 | | - expect(parsed.body[:error]).to eq(body) |
20 | | - expect(parsed.body[:status]).to eq(500) |
21 | | - expect(parsed.body[:original]).to eq(response) |
22 | | - expect(parsed.headers[:status]).to eq(500) |
| 16 | + describe '#get' do |
| 17 | + it 'should call get' do |
| 18 | + @connection.stubs(:get).returns(response).once |
| 19 | + expect(client.get('/test/')).to be_truthy |
| 20 | + end |
| 21 | + end |
| 22 | + |
| 23 | + describe '#post' do |
| 24 | + it 'should call post' do |
| 25 | + @connection.stubs(:post).returns(response).once |
| 26 | + expect(client.post('/test/')).to be_truthy |
| 27 | + end |
| 28 | + end |
| 29 | + |
| 30 | + describe '#put' do |
| 31 | + it 'should call put' do |
| 32 | + @connection.stubs(:put).returns(response).once |
| 33 | + expect(client.put('/test/')).to be_truthy |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + describe '#delete' do |
| 38 | + it 'should call delete' do |
| 39 | + @connection.stubs(:delete).returns(response).once |
| 40 | + expect(client.delete('/test/')).to be_truthy |
| 41 | + end |
| 42 | + end |
| 43 | + |
| 44 | + describe '#save' do |
| 45 | + it 'should post when new' do |
| 46 | + @connection.stubs(:post).returns(response).once |
| 47 | + expect(client.save('/test/', model)).to be_truthy |
| 48 | + end |
| 49 | + |
| 50 | + it 'should put when updating' do |
| 51 | + update = model |
| 52 | + update[:id] = 1 |
| 53 | + @connection.stubs(:put).returns(response).once |
| 54 | + expect(client.save('/test/', update)).to be_truthy |
| 55 | + end |
23 | 56 | end |
24 | 57 | end |
25 | 58 |
|
26 | | - context 'with a successful response' do |
27 | | - it 'should return the parsed body' do |
28 | | - body = {:id => 1, title: 'A post'} |
29 | | - response = OpenStruct.new(status: 200, body: body, headers: { :whatever => "Whatever" }) |
30 | | - expect(client.parse_response(response).to_h).to eq({ body: body, headers: {status: 200}}) |
| 59 | + context 'Record Parsing' do |
| 60 | + let(:body) { "Body" } |
| 61 | + let(:headers) { { } } |
| 62 | + let(:status) { 200 } |
| 63 | + let(:error_status) { 400 } |
| 64 | + let(:response) { OpenStruct.new({body: 'Body', headers: {}, status: 200 } ) } |
| 65 | + |
| 66 | + describe 'parse_response' do |
| 67 | + before(:each) do |
| 68 | + result = stub |
| 69 | + Cortex::Result.any_instance.stubs(:new).with(body, headers, status).returns(true) |
| 70 | + Cortex::Result.any_instance.stubs(:new).with(body, headers, error_status).returns(true) |
| 71 | + end |
| 72 | + |
| 73 | + it 'should construct a successful response' do |
| 74 | + expect(client.parse_response(response)).to be_truthy |
| 75 | + end |
| 76 | + |
| 77 | + it 'should construct an error response' do |
| 78 | + error = response |
| 79 | + error.status = error_status |
| 80 | + expect(client.parse_response(error)).to be_truthy |
| 81 | + end |
31 | 82 | end |
32 | 83 | end |
33 | 84 | end |
0 commit comments