httpspec_simple 0.1.2 → 0.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 92e584f885b0245242709a44891cad5267dc21a3
4
- data.tar.gz: 1570a382d33fca4616b46f542ddc178ce6f2729b
3
+ metadata.gz: cd5d9c54f71ba2d0a77591e82b3151857f9a8dac
4
+ data.tar.gz: 95843a7989ef5a42220ec441c2763eea828bcb85
5
5
  SHA512:
6
- metadata.gz: ba779d59ac4afaa6ca22df2e48ca4d12dd6b975cf1501fa93792fb2079cc9848a403a7c3339d169cf967d79723643e014dcaa6e8342db6980789f058decd922a
7
- data.tar.gz: e68340e72c87f173ecf6a250756933e2ddcbc6a80d5f72841ea53dd89dce346559e7166904fd3a8ae387d11fb292500c2e30e3e5929d134cf3cd2295a1ee88f0
6
+ metadata.gz: d4d9c21b1ee6d3633e2c286594457e848e54f2dcae6b67e175da4eedcab84b80bfd72aa450bd3199530f4600b4b93b64999ea11829402409bf8a6d203d2a55cd
7
+ data.tar.gz: d1a3b4c5951ceab8012add155dcdfc3584d64cf69f89ff00ae64b77c014a90a0b6bc66fab4efbbad354c131d519a8e0c2c69d473816dc0e2aab187dcd8310dff
data/README.md CHANGED
@@ -12,14 +12,30 @@ base_url 'http://kozy4324.github.io'
12
12
  describe request('/') do
13
13
  it { should be_http_ok }
14
14
  it { should respond_within(2).seconds }
15
+ it { should retrieve_body_including '<title>L4L</title>' }
15
16
  end
16
17
 
17
18
  describe request('/blog/archives/') do
18
19
  it { should be_http_ok }
19
20
  it { should respond_within(2).seconds }
21
+ it { should retrieve_body_including '<title>Blog Archive - L4L</title>' }
20
22
  end
21
23
  ```
22
24
 
25
+ ```
26
+ $ rspec -f d
27
+
28
+ http://kozy4324.github.io/
29
+ should be http ok
30
+ should respond within 2 seconds (got 0.122 seconds)
31
+ should retrieve body including "<title>L4L</title>"
32
+
33
+ http://kozy4324.github.io/blog/archives/
34
+ should be http ok
35
+ should respond within 2 seconds (got 0.111 seconds)
36
+ should retrieve body including "<title>Blog Archive - L4L</title>"
37
+ ```
38
+
23
39
  ## Custom matchers
24
40
 
25
41
  ### be_http_ok
@@ -46,13 +62,30 @@ Do request
46
62
 
47
63
  option key | type | description
48
64
  ---------- | ------- | -----------
49
- :retry | integer | when the response code is {40x,50x} or the timeout occurs, retry request the specific times, default value is 0
50
- :timeout | integer | set to Net::HTTP's open_timeout and read_timeout
65
+ :retry | Integer | when the response code is {40x,50x} or the timeout occurs, retry request the specific times, default value is 0
66
+ :timeout | Integer | set to Net::HTTP's open_timeout and read_timeout
67
+ :headers | Hash | set to the request header
51
68
 
52
69
  ### base_url(prepend_string)
53
70
 
54
71
  Prepend to the url string passed to following `request` method
55
72
 
73
+ ### HttpspecSimple::Request.configure
74
+
75
+ configure the global setting as `request()`'s opt argument
76
+
77
+ ```
78
+ HttpspecSimple::Request.configure {|config|
79
+ config.retry = 3
80
+ config.timeout = 15
81
+ config.headers = {"user-agent" => "my-agent"}
82
+ }
83
+ ```
84
+
85
+ ### HttpspecSimple::Request.reset_configuration
86
+
87
+ clear all configuration set by `HttpspecSimple::Request.configure`
88
+
56
89
  ## Contributing
57
90
 
58
91
  1. Fork it
@@ -8,17 +8,19 @@ module HttpspecSimple
8
8
  def initialize(url, opt = {})
9
9
  @url = URI.parse(url)
10
10
  http = Net::HTTP.new(@url.host, @url.port)
11
- unless opt[:timeout].nil?
12
- http.read_timeout = opt[:timeout]
13
- http.open_timeout = opt[:timeout]
14
- end
15
- retry_count = opt[:retry].to_i
16
- res, @response_time = http.start do |http|
17
- process_time do
11
+ http.open_timeout = opt[:timeout] || Request.configuration.timeout
12
+ http.read_timeout = opt[:timeout] || Request.configuration.timeout
13
+ retry_count = (opt[:retry] || Request.configuration.retry).to_i
14
+ res, @response_time = process_time do
15
+ http.start do |http|
18
16
  open_timeout_error = if Net.const_defined?(:OpenTimeout) then Net::OpenTimeout else Timeout::Error end
19
17
  read_timeout_error = if Net.const_defined?(:ReadTimeout) then Net::ReadTimeout else Timeout::Error end
20
18
  begin
21
- res = http.request(Net::HTTP::Get.new(@url.path))
19
+ req = Net::HTTP::Get.new(@url.path)
20
+ if (headers = opt[:headers] || Request.configuration.headers)
21
+ headers.each {|k, v| req[k] = v }
22
+ end
23
+ res = http.request(req)
22
24
  raise RequestError.new if res.kind_of?(Net::HTTPClientError) or res.kind_of?(Net::HTTPServerError)
23
25
  rescue open_timeout_error, read_timeout_error, RequestError
24
26
  retry if (retry_count-=1) > 0
@@ -43,6 +45,26 @@ module HttpspecSimple
43
45
  def to_s
44
46
  @url.to_s
45
47
  end
48
+
49
+ class << Request
50
+ def configure
51
+ config = CONFIG_CLASS.new(20, 0)
52
+ yield config if block_given?
53
+ configuration.timeout = config.timeout
54
+ configuration.retry = config.retry
55
+ configuration.headers = config.headers
56
+ end
57
+
58
+ CONFIG_CLASS = Struct.new(:timeout, :retry, :headers)
59
+
60
+ def configuration
61
+ @config ||= reset_configuration
62
+ end
63
+
64
+ def reset_configuration
65
+ @config = CONFIG_CLASS.new(20, 0, {})
66
+ end
67
+ end
46
68
  end
47
69
 
48
70
  class RequestError < StandardError; end
@@ -1,3 +1,3 @@
1
1
  module HttpspecSimple
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
@@ -57,4 +57,68 @@ describe HttpspecSimple::Request do
57
57
  end
58
58
  response.body.should == "body_string"
59
59
  end
60
+
61
+ describe ".configure" do
62
+ before(:each) { HttpspecSimple::Request.reset_configuration }
63
+ after(:each) { HttpspecSimple::Request.reset_configuration }
64
+ it "should configure the default configuration" do
65
+ HttpspecSimple::Request.configuration.timeout.should == 20
66
+ HttpspecSimple::Request.configuration.retry.should == 0
67
+ HttpspecSimple::Request.configure do |config|
68
+ config.timeout = 60
69
+ config.retry = 10
70
+ end
71
+ HttpspecSimple::Request.configuration.timeout.should == 60
72
+ HttpspecSimple::Request.configuration.retry.should == 10
73
+ end
74
+
75
+ it "should timeout within a specific time" do
76
+ HttpspecSimple::Request.configure do |config|
77
+ config.timeout = 1
78
+ end
79
+ requests, response = server_start( '/' => Proc.new {|req, res| sleep 2 } ) do
80
+ HttpspecSimple::Request.new('http://localhost:10080/')
81
+ end
82
+ response.status.should == "timeout"
83
+ end
84
+
85
+ it "should retry requests while response is not ok" do
86
+ HttpspecSimple::Request.configure do |config|
87
+ config.retry = 3
88
+ end
89
+ response_codes = %w{503 404 200}
90
+ requests, response = server_start( '/' => Proc.new {|req, res| res.status = response_codes.shift } ) do
91
+ HttpspecSimple::Request.new('http://localhost:10080/')
92
+ end
93
+ requests.should have(3).items
94
+ response.status.should == "200"
95
+ end
96
+
97
+ it "should send headers" do
98
+ HttpspecSimple::Request.configure do |config|
99
+ config.headers = {"user-agent" => "my-agent"}
100
+ end
101
+ user_agent_in_req_header = nil
102
+ server_start( '/' => Proc.new {|req, res|
103
+ user_agent_in_req_header = req["user-agent"]
104
+ res.status = "200"
105
+ } ) do
106
+ HttpspecSimple::Request.new('http://localhost:10080/')
107
+ end
108
+ user_agent_in_req_header.should == "my-agent"
109
+ end
110
+ end
111
+
112
+ describe "initialize with :headers option" do
113
+ it "should send headers" do
114
+ user_agent_in_req_header = nil
115
+ server_start( '/' => Proc.new {|req, res|
116
+ user_agent_in_req_header = req["user-agent"]
117
+ res.status = "200"
118
+ } ) do
119
+ HttpspecSimple::Request.new('http://localhost:10080/', :headers => {"user-agent" => "my-agent"})
120
+ end
121
+ user_agent_in_req_header.should == "my-agent"
122
+ end
123
+ end
60
124
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: httpspec_simple
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Koji NAKAMURA