summaryrefslogtreecommitdiff
path: root/test/net/client_test.rb
blob: 098867bfec6e2458a9bc0223e76ab7ac33f4604f (plain)
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
require 'test_helper'

class ClientTest < Minitest::Test
  attr_reader :subject

  def initialize(*args)
    super
    @subject = Net::Hippie::Client.new
    @subject.logger = ENV['CIBUILD'] ? Logger.new('/dev/null') : Logger.new(STDOUT)
  end

  def test_get
    VCR.use_cassette('get_breaches') do
      uri = URI.parse('https://haveibeenpwned.com/api/breaches')
      response = subject.get(uri)
      refute_nil response
      assert_equal(283, JSON.parse(response.body).count)
    end
  end

  def test_multiple_gets_to_pypi
    return

    VCR.use_cassette('multiple-gets-to-pypi') do
      %w{
        https://pypi.org/pypi/django/1.11.3/json
        https://pypi.org/pypi/Django/1.11.3/json
        https://pypi.org/pypi/docutils/0.13.1/json
        https://pypi.org/pypi/pytz/2019.2/json
        https://pypi.org/pypi/requests/2.5.3/json
      }.each do |url|
        subject.follow_redirects = 3
        response = subject.get(url)
        refute_nil response
        assert_equal Net::HTTPOK, response.class
        assert JSON.parse(response.body)
      end
    end
  end

  def test_does_not_follow_redirect
    VCR.use_cassette('does_not_follow_redirect') do
      subject.follow_redirects = 0
      response = subject.get('https://pypi.org/pypi/django/1.11.3/json')
      refute_nil response
      assert_kind_of Net::HTTPRedirection, response
      assert response['location']
    end
  end

  def test_get_with_redirects
    url = 'https://www.example.org/'
    n = 10
    WebMock
      .stub_request(:get, url)
      .to_return(status: 301, headers: { 'Location' => url })
      .times(n)
      .then
      .to_return(status: 200, body: { success: true }.to_json)

    subject.follow_redirects = n
    response = subject.get(url)
    refute_nil response
    assert_equal Net::HTTPOK, response.class
    assert JSON.parse(response.body)['success']
  end

  def test_get_root_path
    VCR.use_cassette('get_root') do
      uri = URI.parse('https://www.mokhan.ca')
      response = subject.get(uri, headers: {})
      refute_nil response
      assert_equal response.code, "200"
      assert response.body.include?("<!DOCTYPE html>")
    end
  end

  def test_get_with_retry
    uri = URI.parse('https://www.example.org/api/scim/v2/schemas')
    WebMock.stub_request(:get, uri.to_s)
      .to_timeout.then
      .to_timeout.then
      .to_timeout.then
      .to_return(status: 200, body: { 'success' => 'true' }.to_json)
    response = subject.with_retry(retries: 3) do |client|
      client.get(uri)
    end
    refute_nil response
    assert_equal Net::HTTPOK, response.class
    assert_equal JSON.parse(response.body)['success'], 'true'
  end

  def test_exceeds_retries
    uri = URI.parse('https://www.example.org/api/scim/v2/schemas')
    WebMock.stub_request(:get, uri.to_s)
      .to_timeout.then
      .to_return(status: 200, body: { 'success' => 'true' }.to_json)

    assert_raises Net::OpenTimeout do
      subject.with_retry(retries: 0) do |client|
        client.get(uri)
      end
    end
  end

  def test_get_with_string_uri
    VCR.use_cassette('get_breaches') do
      response = subject.get('https://haveibeenpwned.com/api/breaches')
      refute_nil response
      assert_equal(283, JSON.parse(response.body).count)
    end
  end

  def test_get_with_generic_uri
    VCR.use_cassette('get_breaches') do
      uri = URI::Generic.build(host: 'haveibeenpwned.com', scheme: 'https', path: '/api/breaches', port: 443)
      response = subject.get(uri)
      refute_nil response
      assert_equal(283, JSON.parse(response.body).count)
    end
  end

  def test_get_with_block_syntax
    VCR.use_cassette('get_breaches') do
      uri = URI.parse('https://haveibeenpwned.com/api/breaches')
      subject.get(uri) do |_request, response|
        @response = response
      end
      refute_nil @response
      assert_equal(283, JSON.parse(@response.body).count)
    end
  end

  def test_get_with_headers
    headers = { 'Accept' => 'application/vnd.haveibeenpwned.v2+json' }
    WebMock.stub_request(:get, 'https://haveibeenpwned.com/api/breaches')
      .with(headers: headers)
      .to_return(status: 201, body: {}.to_json)

    uri = URI.parse('https://haveibeenpwned.com/api/breaches')

    response = subject.get(uri, headers: headers)
    refute_nil response
    assert_equal response.class, Net::HTTPCreated
  end

  def test_post_with_basic_auth_headers
    VCR.use_cassette('post_basic_auth') do
      uri = URI.parse('http://localhost:3000/oauth/tokens')
      client_id = "79a1c787-5cac-4cc5-b00e-374f5a909ef8"
      client_password = "za9NeRkm9YbqKDa6GreUmo6V"
      authorization_code = "NGooUnxYq5f5DHvJqyzDhSft"
      headers = { 'Authorization' => Net::Hippie.basic_auth(client_id, client_password) }
      body = { grant_type: 'authorization_code', code: authorization_code }
      response = subject.post(uri, headers: headers, body: body)
      refute_nil response
      json = JSON.parse(response.body, symbolize_names: true)
      assert_equal('Bearer', json[:token_type])
      assert(json[:token_type])
    end
  end

  def test_get_with_bearer_auth_headers
    VCR.use_cassette('get_bearer_auth') do
      uri = URI.parse('http://localhost:3000/oauth/me')
      token = "eyJhbGciOiJSUzI1NiJ9.eyJleHAiOjE1NDE4NzEyMDksImlhdCI6MTU0MTg2NzYwOSwiaXNzIjoiaHR0cDovL2xvY2FsaG9zdDo1MDAwL21ldGFkYXRhIiwibmJmIjoxNTQxODY3NjA5LCJhdWQiOiI3OWExYzc4Ny01Y2FjLTRjYzUtYjAwZS0zNzRmNWE5MDllZjgiLCJqdGkiOiIzZmZmODNkYi1mMmM3LTRjMDUtYWY4Zi02NWM5ODU1ODUyZjciLCJzdWIiOiIwNDJhZjE5Ny1hOTQxLTRkNTctYjc4Ny00M2IxYmNjZDUwNzAiLCJ0b2tlbl90eXBlIjoiYWNjZXNzIn0.HcwFAgg54RqKEONvHRigAavISuUUmkOA3gz0pkV6UEABCUHGucJCNehnvjpiwe4ZpCF_J6Uen6rfLFQOz7oYe416Du3_lQ3IS3Vc6hTpsT0XZ0bY-BVV_D9-thYFIrT7mNnNoxEs8AhOTBaAjgBammO_097MCwMjTGzAnxm1cmYfBad4yZPJ8HxDqeL769Urc6vz3Ku_M9yUzzfgb6jkwfFlvxmqHOPYWxlDY9uTR2uNr-ZYL5e6J6ZE8rgLkNRqy-jla03z2nFxEuxlSjYbBe60Vcwc4IyKS4QzbKXFXB1v9bKBvJxIUjQPQ7dICQeT9xSXQwDhnBtGUVcGM4njSH0-0rbxE470bGVslmYsChosX0PvRqlo4TMuVr7R5iuwWawZrIB-Dx3kkvhFYhn0jWrEEJkd96nLD-dmg2Tzqa40AE2WqmKtM5jM0LNO9E21l-hPQXAleoKspFIjT6Yd2Om4bJi-0eJB6sNqDuP55rvd5WSjp-ktrrtRirt-9aldCB_0eWP9oFCMJ_Xboq5w0P1W5MXlBv5p6eEdgRjohQyT-dkOvYsZiT9-Y5ggbBMhgtHm1CXZnutN0RE_skZk9PvxV_nUfTji3CHIaUhoJmeI11Tw2tdtOEt58RqkFgK7CZYylG7JqQS6eKpTLB2MrrZtCHY3rrrDaL64l6jYYQI"
      headers = { 'Authorization' => Net::Hippie.bearer_auth(token) }
      response = subject.get(uri, headers: headers)

      refute_nil response
      json = JSON.parse(response.body, symbolize_names: true)
      assert(json[:sub])
      assert(json[:exp])
      assert(json[:iss])
      assert(json[:nbf])
    end
  end

  def test_get_with_body
    uri = URI.parse('https://haveibeenpwned.com/api/breaches')
    body = { 'hello' => 'world' }
    WebMock.stub_request(:get, uri.to_s)
      .with(body: body.to_json)
      .to_return(status: 201, body: {}.to_json)

    response = subject.get(uri, body: body)

    refute_nil response
    assert_equal response.class, Net::HTTPCreated
  end

  def test_post
    VCR.use_cassette('post_breaches') do
      uri = URI.parse('https://haveibeenpwned.com/api/breaches')
      response = subject.post(uri)
      refute_nil response
      assert_equal 'Congratulations!', JSON.parse(response.body)['Message']
    end
  end

  def test_post_with_block_syntax
    VCR.use_cassette('post_breaches') do
      uri = URI.parse('https://haveibeenpwned.com/api/breaches')
      subject.post(uri) do |_request, response|
        @response = response
      end
      refute_nil @response
      assert_equal 'Congratulations!', JSON.parse(@response.body)['Message']
    end
  end

  def test_put
    VCR.use_cassette('put_breaches') do
      uri = URI.parse('https://haveibeenpwned.com/api/breaches')
      body = { command: 'echo hello' }
      response = subject.put(uri, body: body)
      refute_nil response
      assert_equal 'Congratulations!', JSON.parse(response.body)['Message']
    end
  end

  def test_put_with_block_syntax
    VCR.use_cassette('put_breaches') do
      uri = URI.parse('https://haveibeenpwned.com/api/breaches')
      body = { command: 'echo hello' }
      subject.put(uri, body: body) do |_request, response|
        @response = response
      end
      refute_nil @response
      assert_equal 'Congratulations!', JSON.parse(@response.body)['Message']
    end
  end

  def test_delete
    uri = URI.parse('https://haveibeenpwned.com/api/breaches')
    VCR.use_cassette('delete_breaches') do
      response = subject.delete(uri)
      refute_nil response
      assert_equal 'Congratulations!', JSON.parse(response.body)['Message']
    end
  end

  def test_client_tls
    private_key = OpenSSL::PKey::RSA.new(2048)
    certificate = OpenSSL::X509::Certificate.new
    certificate.not_after = certificate.not_before = Time.now
    certificate.public_key = private_key.public_key
    certificate.sign(private_key, OpenSSL::Digest::SHA256.new)

    subject = Net::Hippie::Client.new(certificate: certificate.to_pem, key: private_key.export)
    uri = URI.parse('https://haveibeenpwned.com/api/breaches')

    @called = false
    VCR.use_cassette('get_breaches') do
      subject.get(uri) do |_request, response|
        @called = true
        refute_nil response
        assert_equal '000webhost', JSON.parse(response.body)[0]['Title']
      end
    end
    assert(@called)
  end

  def test_client_tls_with_passphrase
    private_key = OpenSSL::PKey::RSA.new(2048)
    passphrase = SecureRandom.hex(16)
    certificate = OpenSSL::X509::Certificate.new
    certificate.not_after = certificate.not_before = Time.now
    certificate.public_key = private_key.public_key
    certificate.sign(private_key, OpenSSL::Digest::SHA256.new)

    subject = Net::Hippie::Client.new(
      certificate: certificate.to_pem,
      key: private_key.export(OpenSSL::Cipher.new('AES-256-CBC'), passphrase),
      passphrase: passphrase
    )
    uri = URI.parse('https://haveibeenpwned.com/api/breaches')

    @called = false
    VCR.use_cassette('get_breaches') do
      subject.get(uri) do |_request, response|
        @called = true
        refute_nil response
        assert_equal '000webhost', JSON.parse(response.body)[0]['Title']
      end
    end
    assert(@called)
  end

  def test_open_timeout_setting
    assert_nil subject.open_timeout
    @subject.open_timeout = 10
    assert_equal subject.open_timeout, 10
  end
end