forked from freeCodeCamp/devdocs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_test.rb
More file actions
69 lines (57 loc) · 1.67 KB
/
Copy pathrequest_test.rb
File metadata and controls
69 lines (57 loc) · 1.67 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
require 'test_helper'
require 'docs'
class DocsRequestTest < MiniTest::Spec
let :url do
'http://example.com'
end
def request(url = self.url, options = {})
Docs::Request.new(url, options).tap do |request|
request.extend FakeInstrumentation
end
end
let :response do
Typhoeus::Response.new.tap do |response|
Typhoeus.stub(url).and_return(response)
end
end
after do
Typhoeus::Expectation.clear
end
describe ".run" do
before { response }
it "makes a request and returns the response" do
assert_equal response, Docs::Request.run(url)
end
it "calls the given block with the response" do
Docs::Request.run(url) { |arg| @arg = arg }
assert_equal response, @arg
end
end
describe ".new" do
it "accepts a Docs::URL" do
url = Docs::URL.parse 'http://example.com'
assert_equal url.to_s, request(url).base_url
end
it "defaults :followlocation to true" do
assert request.options[:followlocation]
refute request(url, followlocation: false).options[:followlocation]
end
end
describe "#run" do
before { response }
it "instruments 'response'" do
req = request.tap(&:run)
assert req.last_instrumentation
assert_equal 'response.request', req.last_instrumentation[:event]
assert_equal url, req.last_instrumentation[:payload][:url]
assert_equal response, req.last_instrumentation[:payload][:response]
end
end
describe "#response=" do
it "extends the object with Docs::Response" do
response = Object.new
request.response = response
assert_includes response.singleton_class.ancestors, Docs::Response
end
end
end