spec/client_spec.rb in bootic_client-0.0.11 vs spec/client_spec.rb in bootic_client-0.0.12
- old
+ new
@@ -10,14 +10,15 @@
let(:request_headers) {
{'Authorization' => "Bearer xxx"}
}
let(:response_headers) {
{
- 'Content-Type' => 'application/json',
+ 'Content-Type' => 'application/json',
'Last-Modified' => 'Sat, 07 Jun 2014 12:10:33 GMT',
'ETag' => '0937dafce10db7b7d405667f9576d26d',
- 'Cache-Control' => 'max-age=0, private, must-revalidate'
+ 'Cache-Control' => 'max-age=0, private, must-revalidate',
+ 'Vary' => 'Acept-Encoding,Authorization'
}
}
let(:root_data) {
{
'_links' => {
@@ -26,42 +27,66 @@
'message' => "Hello!"
}
}
describe '#get' do
+ def assert_successful_response(response)
+ expect(response).to be_kind_of(Faraday::Response)
+ expect(response.status).to eql(200)
+ response.body.tap do |b|
+ expect(b['_links']['shops']).to eql({'href' => 'https://api.bootic.net/v1/products'})
+ end
+ end
+ context 'switching cache key as per Vary header' do
+ let!(:req) {
+ stub_request(:get, root_url)
+ .to_return(status: 200, body: JSON.dump(root_data), headers: response_headers.merge('Cache-Control' => 'max-age=100'))
+ }
+
+ before do
+ client.get(root_url, {}, request_headers.merge('Authorization' => 'Bearer aaa'))
+ end
+
+ it 'is cached when using the same authorization' do
+ resp = client.get(root_url, {}, request_headers.merge('Authorization' => 'Bearer aaa'))
+ expect(req).to have_been_requested.once
+ assert_successful_response resp
+ end
+
+ it 'is not cached when using a different authorization' do
+ resp = client.get(root_url, {}, request_headers.merge('Authorization' => 'Bearer bbb'))
+ expect(req).to have_been_requested.twice
+ assert_successful_response resp
+ end
+
+ end
+
context 'fresh' do
before do
stub_request(:get, root_url)
.to_return(status: 200, body: JSON.dump(root_data), headers: response_headers)
end
let!(:response) { client.get(root_url, {}, request_headers) }
it 'returns parsed Faraday response' do
- expect(response).to be_kind_of(Faraday::Response)
- expect(response.status).to eql(200)
- response.body.tap do |b|
- expect(b['_links']['shops']).to eql({'href' => 'https://api.bootic.net/v1/products'})
- end
+ assert_successful_response response
end
context 'and then cached' do
before do
@cached_request = stub_request(:get, root_url)
.with(headers: {'If-Modified-Since' => 'Sat, 07 Jun 2014 12:10:33 GMT'})
.to_return(status: 304, body: '', headers: response_headers)
end
it 'returns cached response' do
- r = client.get(root_url, {}, request_headers)
+ resp = client.get(root_url, {}, request_headers)
expect(@cached_request).to have_been_requested
- expect(r.status).to eql(200)
- r.body.tap do |b|
- expect(b['_links']['shops']).to eql({'href' => 'https://api.bootic.net/v1/products'})
- end
+ assert_successful_response resp
end
end
context 'and then cached by ETag' do
before do
@@ -69,18 +94,16 @@
.with(headers: {'If-None-Match' => response_headers['ETag']})
.to_return(status: 304, body: '', headers: response_headers)
end
it 'returns cached response' do
- r = client.get(root_url, {}, request_headers)
+ resp = client.get(root_url, {}, request_headers)
expect(@cached_request).to have_been_requested
- expect(r.status).to eql(200)
- r.body.tap do |b|
- expect(b['_links']['shops']).to eql({'href' => 'https://api.bootic.net/v1/products'})
- end
+ assert_successful_response resp
end
end
+
end
context 'errors' do
describe '500 Server error' do
before do