-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient_spec.rb
More file actions
97 lines (76 loc) · 2.56 KB
/
Copy pathclient_spec.rb
File metadata and controls
97 lines (76 loc) · 2.56 KB
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
RSpec.describe GoTransit::Client do
describe "#get" do
it "uses a custom url if configured" do
GoTransit.configure do |config|
config.custom_base_url = "https://example.com/OpenDataAPI/api"
end
client = GoTransit::Client.new
allow(Net::HTTP).to receive(:get_response).and_call_original
client.get("Stop/Details/UN")
expect(Net::HTTP).to have_received(:get_response).
with(URI.parse("https://example.com/OpenDataAPI/api/V1/Stop/"\
"Details/UN?key=")).once
end
it "returns a response from the metrolinx API" do
client = GoTransit::Client.new
result = client.get("Stop/Details/UN")
expect(result).to be_kind_of(GoTransit::Response)
end
context "when the API returns a non 200 code" do
it "responds to a 204 error" do
client = GoTransit::Client.new
expect {
client.get("NoContent")
}.to raise_error(GoTransit::NoContentError)
end
it "responds to a 400 error" do
client = GoTransit::Client.new
expect {
client.get("BadRequest")
}.to raise_error(GoTransit::BadRequestError)
end
it "responds to a 401 error" do
client = GoTransit::Client.new
expect {
client.get("Unauthorized")
}.to raise_error(GoTransit::UnauthorizedError)
end
it "responds to a 403 error" do
client = GoTransit::Client.new
expect {
client.get("Forbidden")
}.to raise_error(GoTransit::ForbiddenError)
end
it "responds to a 404 error" do
client = GoTransit::Client.new
expect {
client.get("NotFound")
}.to raise_error(GoTransit::NotFoundError)
end
end
context "when log response is enabled" do
it "logs a debug message" do
logger = Logger.new(@stdout)
allow(logger).to receive(:debug)
GoTransit.configure do |config|
config.log_response = true
config.logger = logger
end
client = GoTransit::Client.new
client.get("Stop/Details/UN")
expect(logger).to have_received(:debug).twice
end
it "logs the status code" do
logger = Logger.new(@stdout)
allow(logger).to receive(:debug)
GoTransit.configure do |config|
config.log_response = true
config.logger = logger
end
client = GoTransit::Client.new
client.get("Stop/Details/UN")
expect(logger).to have_received(:debug).with("[HTTP Response 200]").once
end
end
end
end