aitch 0.5.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +3 -5
  3. data/CHANGELOG.md +38 -0
  4. data/README.md +50 -14
  5. data/Rakefile +10 -0
  6. data/aitch.gemspec +4 -2
  7. data/lib/aitch.rb +7 -2
  8. data/lib/aitch/configuration.rb +1 -12
  9. data/lib/aitch/dsl.rb +1 -0
  10. data/lib/aitch/errors.rb +1 -0
  11. data/lib/aitch/ext/to_query.rb +1 -0
  12. data/lib/aitch/location.rb +3 -2
  13. data/lib/aitch/namespace.rb +1 -0
  14. data/lib/aitch/redirect.rb +8 -2
  15. data/lib/aitch/request.rb +22 -9
  16. data/lib/aitch/response.rb +4 -13
  17. data/lib/aitch/response/body.rb +1 -0
  18. data/lib/aitch/response/description.rb +52 -51
  19. data/lib/aitch/response/errors.rb +1 -0
  20. data/lib/aitch/response_parser.rb +29 -0
  21. data/lib/aitch/response_parser/default_parser.rb +18 -0
  22. data/lib/aitch/response_parser/html_parser.rb +18 -0
  23. data/lib/aitch/response_parser/json_parser.rb +18 -0
  24. data/lib/aitch/response_parser/xml_parser.rb +18 -0
  25. data/lib/aitch/uri.rb +1 -0
  26. data/lib/aitch/utils.rb +1 -0
  27. data/lib/aitch/version.rb +2 -1
  28. data/test/aitch/aitch_test.rb +27 -0
  29. data/test/aitch/configuration_test.rb +29 -0
  30. data/test/aitch/dsl_test.rb +48 -0
  31. data/test/aitch/execute_test.rb +45 -0
  32. data/test/aitch/namespace_test.rb +12 -0
  33. data/test/aitch/request/client_https_test.rb +20 -0
  34. data/test/aitch/request/follow_redirect_test.rb +64 -0
  35. data/test/aitch/request/request_class_test.rb +28 -0
  36. data/test/aitch/request/status_code_validation_test.rb +19 -0
  37. data/test/aitch/request_test.rb +138 -0
  38. data/test/aitch/response/custom_response_parser_test.rb +31 -0
  39. data/test/aitch/response/errors_test.rb +16 -0
  40. data/test/aitch/response/html_response_test.rb +18 -0
  41. data/test/aitch/response/json_response_test.rb +18 -0
  42. data/test/aitch/response/raw_response_test.rb +11 -0
  43. data/test/aitch/response/status_3xx_test.rb +56 -0
  44. data/test/aitch/response/status_4xx_test.rb +18 -0
  45. data/test/aitch/response/status_5xx_test.rb +18 -0
  46. data/test/aitch/response/xml_response_test.rb +18 -0
  47. data/test/aitch/response_parser/html_parser_test.rb +9 -0
  48. data/test/aitch/response_parser/json_parser_test.rb +9 -0
  49. data/test/aitch/response_parser/xml_parser_test.rb +17 -0
  50. data/test/aitch/response_test.rb +114 -0
  51. data/test/aitch/uri_test.rb +37 -0
  52. data/test/aitch/utils/symbolize_keys_test.rb +9 -0
  53. data/test/aitch/utils/underscore_test.rb +12 -0
  54. data/{spec → test}/fixtures/iso8859-1.xml +0 -0
  55. data/{spec/support/request_uri.rb → test/support/helpers.rb} +12 -3
  56. data/test/test_helper.rb +15 -0
  57. metadata +70 -37
  58. data/.rspec +0 -1
  59. data/lib/aitch/html_parser.rb +0 -7
  60. data/lib/aitch/xml_parser.rb +0 -7
  61. data/spec/aitch/aitch_spec.rb +0 -71
  62. data/spec/aitch/configuration_spec.rb +0 -33
  63. data/spec/aitch/dsl_spec.rb +0 -47
  64. data/spec/aitch/html_parser_spec.rb +0 -8
  65. data/spec/aitch/namespace_spec.rb +0 -11
  66. data/spec/aitch/request_spec.rb +0 -261
  67. data/spec/aitch/response_spec.rb +0 -259
  68. data/spec/aitch/uri_spec.rb +0 -36
  69. data/spec/aitch/utils_spec.rb +0 -19
  70. data/spec/aitch/xml_parser_spec.rb +0 -16
  71. data/spec/spec_helper.rb +0 -24
  72. data/spec/support/webmock.rb +0 -15
@@ -1,259 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Aitch::Response do
4
- it "has body" do
5
- register_uri(:get, "http://example.org/", body: "Hello")
6
- response = Aitch.get("http://example.org/")
7
- expect(response.body).to eq("Hello")
8
- end
9
-
10
- it "sets current url" do
11
- register_uri(:get, "http://example.org/", body: "Hello")
12
- response = Aitch.get("http://example.org/")
13
- expect(response.url).to eq("http://example.org/")
14
- end
15
-
16
- it "parses gzip response" do
17
- stdio = StringIO.new
18
- gzipped = Zlib::GzipWriter.new(stdio)
19
- gzipped.write("Hello")
20
- gzipped.finish
21
-
22
- register_uri(:get, "http://example.org/", body: stdio.string, content_encoding: "gzip")
23
- response = Aitch.get("http://example.org/")
24
-
25
- expect(response.body).to eq("Hello")
26
- end
27
-
28
- it "deflates response" do
29
- stdio = StringIO.new
30
- deflated = Zlib::Deflate.deflate("Hello")
31
-
32
- register_uri(:get, "http://example.org/", body: deflated, content_encoding: "deflate")
33
- response = Aitch.get("http://example.org/")
34
-
35
- expect(response.body).to eq("Hello")
36
- end
37
-
38
- it "returns status code" do
39
- register_uri(:get, "http://example.org/", body: "")
40
- response = Aitch.get("http://example.org/")
41
- expect(response.code).to eq(200)
42
- end
43
-
44
- it "returns content type" do
45
- register_uri(:get, "http://example.org/", content_type: "text/html")
46
- response = Aitch.get("http://example.org/")
47
- expect(response.content_type).to eq("text/html")
48
- end
49
-
50
- it "detects as successful response" do
51
- register_uri(:get, "http://example.org/", content_type: "text/html")
52
- response = Aitch.get("http://example.org/")
53
- expect(response).to be_success
54
- end
55
-
56
- it "returns headers" do
57
- register_uri(:get, "http://example.org/", content_type: "text/html")
58
- headers = Aitch.get("http://example.org/").headers
59
-
60
- expect(headers).to be_a(Hash)
61
- expect(headers["content-type"]).to eq("text/html")
62
- end
63
-
64
- it "normalizes custom headers" do
65
- register_uri(:get, "http://example.org/", headers: {"X-Runtime" => "0.003"})
66
- headers = Aitch.get("http://example.org/").headers
67
-
68
- expect(headers["runtime"]).to eq("0.003")
69
- end
70
-
71
- it "maps missing methods to headers" do
72
- register_uri(:get, "http://example.org/", headers: {"X-Runtime" => "0.003"})
73
- response = Aitch.get("http://example.org/")
74
-
75
- expect(response.runtime).to eq("0.003")
76
- expect(response).to respond_to(:runtime)
77
- end
78
-
79
- it "raises when have no method" do
80
- register_uri(:get, "http://example.org/")
81
- response = Aitch.get("http://example.org/")
82
-
83
- expect {
84
- response.runtime
85
- }.to raise_error(NoMethodError)
86
- end
87
-
88
- it "returns description for 200 OK" do
89
- register_uri(:get, "http://example.org/", status: 200)
90
- response = Aitch.get("http://example.org/")
91
-
92
- expect(response.description).to eq("200 OK")
93
- end
94
-
95
- it "returns description for 101 Switch Protocol" do
96
- register_uri(:get, "http://example.org/", status: 101)
97
- response = Aitch.get("http://example.org/")
98
-
99
- expect(response.description).to eq("101 Switch Protocol")
100
- end
101
-
102
- it "returns description for 444 No Response (nginx)" do
103
- register_uri(:get, "http://example.org/", status: 444)
104
- response = Aitch.get("http://example.org/")
105
-
106
- expect(response.description).to eq("444")
107
- end
108
-
109
- it "overrides inspect" do
110
- register_uri(:get, "http://example.org/", status: 101, content_type: "text/html")
111
- response = Aitch.get("http://example.org/")
112
-
113
- expect(response.inspect).to eq("#<Aitch::Response 101 Switch Protocol (text/html)>")
114
- end
115
-
116
- context "status 3xx" do
117
- before { Aitch.configuration.follow_redirect = false }
118
-
119
- it "has body" do
120
- register_uri(:get, "http://example.org/", body: "Hello", status: 301)
121
- response = Aitch.get("http://example.org/")
122
- expect(response.body).to eq("Hello")
123
- end
124
-
125
- it "detects as successful response" do
126
- register_uri(:get, "http://example.org/", status: 301)
127
- response = Aitch.get("http://example.org/")
128
-
129
- expect(response).to be_success
130
- expect(response).to be_ok
131
- end
132
-
133
- it "detects as redirect" do
134
- register_uri(:get, "http://example.org/", status: 301)
135
- response = Aitch.get("http://example.org/")
136
- expect(response).to be_redirect
137
- end
138
-
139
- it "returns location" do
140
- register_uri(:get, "http://example.org/", status: 301, location: "https://example.com/")
141
- response = Aitch.get("http://example.org/")
142
- expect(response.location).to eq("https://example.com/")
143
- end
144
-
145
- it "follows absolute paths" do
146
- Aitch.configuration.follow_redirect = true
147
- Aitch.configuration.redirect_limit = 5
148
-
149
- register_uri(:get, "http://example.org/", status: 301, location: "/hello")
150
- register_uri(:get, "http://example.org/hello", status: 301, location: "/hi")
151
- register_uri(:get, "http://example.org/hi", status: 200, body: "Hi")
152
-
153
- response = Aitch.get("http://example.org/")
154
-
155
- expect(response.redirected_from).to eq(["http://example.org/", "http://example.org/hello"])
156
- expect(response.url).to eq("http://example.org/hi")
157
- expect(response.body).to eq("Hi")
158
- end
159
- end
160
-
161
- context "status 4xx" do
162
- it "detects as error" do
163
- register_uri(:get, "http://example.org/", status: 404)
164
- response = Aitch.get("http://example.org/")
165
-
166
- expect(response).to be_error
167
- end
168
-
169
- it "sets error" do
170
- register_uri(:get, "http://example.org/", status: 404)
171
- response = Aitch.get("http://example.org/")
172
-
173
- expect(response.error).to eq(Aitch::NotFoundError)
174
- end
175
- end
176
-
177
- context "status 5xx" do
178
- it "detects as error" do
179
- register_uri(:get, "http://example.org/", status: 500)
180
- response = Aitch.get("http://example.org/")
181
-
182
- expect(response).to be_error
183
- end
184
-
185
- it "sets error" do
186
- register_uri(:get, "http://example.org/", status: 500)
187
- response = Aitch.get("http://example.org/")
188
-
189
- expect(response.error).to eq(Aitch::InternalServerErrorError)
190
- end
191
- end
192
-
193
- context "raw body" do
194
- it "returns as it is" do
195
- register_uri(:get, "http://example.org/", body: "HELLO", content_type: "text/plain")
196
- response = Aitch.get("http://example.org/")
197
- expect(response.body).to eq("HELLO")
198
- end
199
- end
200
-
201
- context "JSON" do
202
- it "detects as json" do
203
- register_uri(:get, "http://example.org/", body: "[]", content_type: "application/json")
204
- response = Aitch.get("http://example.org/")
205
-
206
- expect(response).to be_json
207
- end
208
-
209
- it "returns json" do
210
- register_uri(:get, "http://example.org/", body: "[1,2,3]", content_type: "application/json")
211
- response = Aitch.get("http://example.org/")
212
-
213
- expect(response.json).to eq([1,2,3])
214
- end
215
- end
216
-
217
- context "HTML" do
218
- it "detects as html" do
219
- register_uri(:get, "http://example.org/", body: "", content_type: "text/html")
220
- response = Aitch.get("http://example.org/")
221
-
222
- expect(response).to be_html
223
- end
224
-
225
- it "returns html" do
226
- register_uri(:get, "http://example.org/", body: "Hello", content_type: "text/html")
227
- response = Aitch.get("http://example.org/")
228
-
229
- expect(response.html).to be_a(Nokogiri::HTML::Document)
230
- end
231
- end
232
-
233
- context "XML" do
234
- it "detects as xml" do
235
- register_uri(:get, "http://example.org/", body: "[]", content_type: "application/xml")
236
- response = Aitch.get("http://example.org/")
237
-
238
- expect(response).to be_xml
239
- end
240
-
241
- it "returns xml" do
242
- register_uri(:get, "http://example.org/", body: "<foo/>", content_type: "application/xml")
243
- response = Aitch.get("http://example.org/")
244
-
245
- expect(response.xml).to be_a(Nokogiri::XML::Document)
246
- end
247
- end
248
-
249
- Aitch::Response::ERRORS.each do |code, exception|
250
- name = Aitch::Utils.underscore(exception.name.split("::").last).gsub("_error", "")
251
-
252
- it "detects response as #{name}" do
253
- config = double
254
- http_response = double(code: code)
255
- response = Aitch::Response.new(config, http_response)
256
- expect(response.public_send("#{name}?")).to be_truthy
257
- end
258
- end
259
- end
@@ -1,36 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Aitch::URI do
4
- it "returns default path" do
5
- expect(Aitch::URI.new("http://example.org").path).to eq("/")
6
- end
7
-
8
- it "returns defined path" do
9
- expect(Aitch::URI.new("http://example.org/some/path").path).to eq("/some/path")
10
- end
11
-
12
- it "returns fragment" do
13
- expect(Aitch::URI.new("http://example.org/#top").fragment).to eq("#top")
14
- end
15
-
16
- it "returns query string" do
17
- expect(Aitch::URI.new("http://example.org/?a=1&b=2").query).to eq("?a=1&b=2")
18
- end
19
-
20
- it "converts data into query string" do
21
- expect(Aitch::URI.new("http://example.org", a: 1, b: 2).query).to eq("?a=1&b=2")
22
- end
23
-
24
- it "merges data into query string" do
25
- expect(Aitch::URI.new("http://example.org/?a=1&b=2", c: 3).query).to eq("?a=1&b=2&c=3")
26
- end
27
-
28
- it "ignores data when request has body" do
29
- expect(Aitch::URI.new("http://example.org/", {c: 3}, true).query).to eq(nil)
30
- end
31
-
32
- it "returns request uri" do
33
- uri = Aitch::URI.new("http://example.org/some/path?a=1&b=2#hello", c: 3)
34
- expect(uri.request_uri).to eq("/some/path?a=1&b=2&c=3#hello")
35
- end
36
- end
@@ -1,19 +0,0 @@
1
- require "spec_helper"
2
-
3
- describe Aitch::Utils do
4
- describe ".underscore" do
5
- it "replaces capital letters by underscores" do
6
- expect(Aitch::Utils.underscore("SomeConstantName")).to eq("some_constant_name")
7
- end
8
-
9
- it "considers URI acronym" do
10
- expect(Aitch::Utils.underscore("RequestURITooLong")).to eq("request_uri_too_long")
11
- end
12
- end
13
-
14
- describe ".symbolize_keys" do
15
- it "converts keys to symbols" do
16
- expect(Aitch::Utils.symbolize_keys("a" => 1)).to eq(a: 1)
17
- end
18
- end
19
- end
@@ -1,16 +0,0 @@
1
- # -*- encoding: utf-8 -*-
2
- require "spec_helper"
3
-
4
- describe Aitch::XMLParser do
5
- it "instantiates Nokogiri" do
6
- expect(Nokogiri).to receive(:XML).with("XML", nil, "utf-8")
7
- Aitch::XMLParser.load("XML")
8
- end
9
-
10
- it "converts ISO-8859-1 to UTF-8" do
11
- xml = Aitch::XMLParser.load(File.read("./spec/fixtures/iso8859-1.xml"))
12
-
13
- expect(xml.encoding).to eq("utf-8")
14
- expect(xml.to_xml).to include(%[<?xml version="1.0" encoding="utf-8"?>])
15
- end
16
- end
@@ -1,24 +0,0 @@
1
- require "codeclimate-test-reporter"
2
- CodeClimate::TestReporter.start
3
-
4
- require "bundler/setup"
5
- Bundler.require
6
-
7
- require "aitch"
8
- require "base64"
9
- require "test_notifier/runner/rspec"
10
- require "webmock/rspec"
11
- require "nokogiri"
12
-
13
- require_relative "support/webmock"
14
- require_relative "support/request_uri"
15
-
16
- RSpec.configure do |config|
17
- config.filter_run_excluding :ruby => -> version {
18
- !(RUBY_VERSION.to_s =~ /^#{version.to_s}/)
19
- }
20
-
21
- config.expect_with :rspec do |c|
22
- c.syntax = :expect
23
- end
24
- end
@@ -1,15 +0,0 @@
1
- WebMock.disable_net_connect!(allow: /codeclimate\.com/)
2
-
3
- def WebMock.requests
4
- @requests ||= []
5
- end
6
-
7
- WebMock.after_request do |request, response|
8
- WebMock.requests << request
9
- end
10
-
11
- RSpec.configure do |config|
12
- config.before(:each) do
13
- WebMock.requests.clear
14
- end
15
- end