forked from NEWROPE/scnnr-ruby
-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection_spec.rb
125 lines (100 loc) · 3.76 KB
/
connection_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe Scnnr::Connection do
before { stub_request(method, uri).to_return(body: expected_body, status: 200) }
let(:connection) { described_class.new(uri, method, api_key, logger) }
let(:uri) { URI.parse('https://dummy.scnnr.cubki.jp') }
let(:logger) { Logger.new('/dev/null') }
let(:api_key) { nil }
let(:expected_body) { fixture('queued_recognition.json').read }
describe '#send_request' do
subject { connection.send_request(&block) }
let(:method) { %i[get post].sample }
let(:block) { nil }
context 'when the api_key is not set' do
it do
expect(subject).to be_a Net::HTTPSuccess
expect(subject.body).to eq expected_body
expect(WebMock).to have_requested(method, uri)
end
end
context 'when the api_key is set' do
let(:api_key) { 'dummy_key' }
let(:requested_options) { { headers: { 'x-api-key' => api_key } } }
it do
expect(subject).to be_a Net::HTTPSuccess
expect(subject.body).to eq expected_body
expect(WebMock).to have_requested(method, uri).with(requested_options)
end
end
context 'when passing block' do
let(:block) { ->(request) { request.content_type = requested_content_type } }
let(:requested_content_type) { 'application/json' }
let(:requested_options) do
{ headers: { 'Content-Type' => requested_content_type } }
end
it do
expect(subject).to be_a Net::HTTPSuccess
expect(subject.body).to eq expected_body
expect(WebMock).to have_requested(method, uri).with(requested_options)
end
end
context 'when the response is an error' do
let(:response_error) { described_class::RETRY_ERROR_CLASSES.sample }
before do
retry_count = 0
allow_any_instance_of(described_class).to receive(:sleep)
allow(Net::HTTP).to receive(:start) do
retry_count += 1
next Net::HTTPSuccess.new(nil, nil, nil) if retry_count > 1 && success_at_retry?
raise(response_error)
end
end
context 'and it succeeds after retrying' do
let(:success_at_retry?) { true }
it { is_expected.to be_a Net::HTTPSuccess }
end
context 'and it does not succeed after retrying' do
let(:success_at_retry?) { false }
it { expect { subject }.to raise_error(response_error) }
end
end
end
describe '#send_stream' do
subject { connection.send_stream(image) }
let(:method) { :post }
let(:api_key) { 'dummy_key' }
let(:image) { fixture('images/sample.png') }
let(:requested_content_type) { 'application/octet-stream' }
let(:requested_options) do
{
headers: { 'x-api-key' => api_key, 'Content-Type' => requested_content_type, 'Transfer-Encoding' => 'chunked' },
}
end
it do
# can not test checking requested body_stream with WebMock, so instead.
expect_any_instance_of(Net::HTTP::Post).to receive(:body_stream=).with(image)
expect(subject).to be_a Net::HTTPSuccess
expect(subject.body).to eq expected_body
expect(WebMock).to have_requested(method, uri).with(requested_options)
end
end
describe '#send_json' do
subject { connection.send_json(data) }
let(:method) { :post }
let(:api_key) { 'dummy_key' }
let(:data) { { data: 'dummy_data' } }
let(:requested_content_type) { 'application/json' }
let(:requested_options) do
{
headers: { 'x-api-key' => api_key, 'Content-Type' => requested_content_type },
body: data.to_json,
}
end
it do
expect(subject).to be_a Net::HTTPSuccess
expect(subject.body).to eq expected_body
expect(WebMock).to have_requested(method, uri).with(requested_options)
end
end
end