|
3 | 3 | describe Cortex::Request do |
4 | 4 | let(:client) { Cortex::Client.new(access_token: '123') } |
5 | 5 |
|
6 | | - before(:context) do |
7 | | - connection = stub |
8 | | - connection.stubs(:get).returns(OpenStruct.new({body: "Body", headers: {}, status: 200} ) ) |
| 6 | + context 'Request Methods' do |
| 7 | + let(:response) { OpenStruct.new({body: "Body", headers: {}, status: 200} ) } |
| 8 | + let(:model) { { title: 'Test' } } |
9 | 9 |
|
10 | | - Cortex::Client.any_instance.stubs(:connection).returns(connection) |
11 | | - end |
| 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) |
| 14 | + end |
12 | 15 |
|
13 | | - describe 'get' do |
14 | | - it 'should work' do |
15 | | - client.get('/test/') |
| 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 '#delete!' do |
| 45 | + it 'should be true when status is under 300' do |
| 46 | + @connection.stubs(:delete).returns(response).once |
| 47 | + expect(client.delete!('/test/')).to be_truthy |
| 48 | + end |
| 49 | + |
| 50 | + it 'should be false when status is over 300' do |
| 51 | + error = response |
| 52 | + error.status = 403 |
| 53 | + @connection.stubs(:delete).returns(error).once |
| 54 | + expect(client.delete!('/test/')).to be_falsey |
| 55 | + end |
16 | 56 | end |
17 | | - end |
18 | 57 |
|
19 | | - context 'with a non-cortex error response' do |
20 | | - it 'should return a wrapped response' do |
21 | | - body = 'Catastrophic error' |
22 | | - response = OpenStruct.new(status: 500, body: body, headers: { whatever: "Whatever", status: 500 } ) |
23 | | - parsed = client.parse_response(response) |
24 | | - expect(parsed.contents).to eq(body) |
25 | | - expect(parsed.raw_headers[:status]).to eq(500) |
| 58 | + describe '#save' do |
| 59 | + it 'should post when new' do |
| 60 | + @connection.stubs(:post).returns(response).once |
| 61 | + expect(client.save('/test/', model)).to be_truthy |
| 62 | + end |
| 63 | + |
| 64 | + it 'should put when updating' do |
| 65 | + update = model |
| 66 | + update[:id] = 1 |
| 67 | + @connection.stubs(:put).returns(response).once |
| 68 | + expect(client.save('/test/', update)).to be_truthy |
| 69 | + end |
26 | 70 | end |
27 | 71 | end |
28 | 72 |
|
29 | | - context 'with a successful response' do |
30 | | - it 'should return the parsed body' do |
31 | | - body = {:id => 1, title: 'A post'} |
32 | | - response = OpenStruct.new(status: 200, body: body, headers: { :whatever => "Whatever" }) |
33 | | - expect(client.parse_response(response)).to eq({ body: body, headers: {status: 200}}) |
| 73 | + context 'Record Parsing' do |
| 74 | + let(:body) { "Body" } |
| 75 | + let(:headers) { { } } |
| 76 | + let(:status) { 200 } |
| 77 | + let(:error_status) { 400 } |
| 78 | + let(:response) { OpenStruct.new({body: 'Body', headers: {}, status: 200 } ) } |
| 79 | + |
| 80 | + describe 'parse_response' do |
| 81 | + before(:each) do |
| 82 | + result = stub |
| 83 | + Cortex::Result.any_instance.stubs(:new).with(body, headers, status).returns(true) |
| 84 | + Cortex::Result.any_instance.stubs(:new).with(body, headers, error_status).returns(true) |
| 85 | + end |
| 86 | + |
| 87 | + it 'should construct a successful response' do |
| 88 | + expect(client.parse_response(response)).to be_truthy |
| 89 | + end |
| 90 | + |
| 91 | + it 'should construct an error response' do |
| 92 | + error = response |
| 93 | + error.status = error_status |
| 94 | + expect(client.parse_response(error)).to be_truthy |
| 95 | + end |
34 | 96 | end |
35 | 97 | end |
36 | 98 | end |
0 commit comments