httpspec_simple 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 5514ba53d9108406aac1f80530b6926bddbedb58
4
+ data.tar.gz: 273a467cd18a9e54e10dd80266202167ab9a2889
5
+ SHA512:
6
+ metadata.gz: 1120cc25e2addd35c536fb37f60936538193b4cda2bfedff8c35abcbe76a1e056ca6db2814ccf225df11dcebda2ac2c0e51fbfd176aec693059d14f80ed16e34
7
+ data.tar.gz: 71bc7074b5b87acf5a1c687ef923898c9e14449fd07d9ab068725766b68ddfb9a753fffd38708f2a5f2dda4e150bdabb8d6a3aa6be13b6b66f01571e64359f23
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ vendor
19
+ *.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format progress
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in httpspec_simple.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Koji NAKAMURA
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # HttpspecSimple
2
+
3
+ RSpec extension for HTTP request
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ require 'httpspec_simple'
9
+
10
+ base_url 'http://kozy4324.github.io'
11
+
12
+ describe request('/') do
13
+ it { should be_http_ok }
14
+ it { should respond_within(2).seconds }
15
+ end
16
+
17
+ describe request('/blog/archives/') do
18
+ it { should be_http_ok }
19
+ it { should respond_within(2).seconds }
20
+ end
21
+ ```
22
+
23
+ ## Custom matcher
24
+
25
+ ### be_http_ok
26
+
27
+ Example passes if response is 200 ok
28
+
29
+ ### resond_within(n).seconds
30
+
31
+ Example passes if response time is less than n seconds
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'httpspec_simple/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "httpspec_simple"
8
+ spec.version = HttpspecSimple::VERSION
9
+ spec.authors = ["Koji NAKAMURA"]
10
+ spec.email = ["kozy4324@gmail.com"]
11
+ spec.description = %q{RSpec extension for HTTP request}
12
+ spec.summary = %q{RSpec extension for HTTP request}
13
+ spec.homepage = "https://github.com/kozy4324/httpspec_simple"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rspec", "~> 2.14.1"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.3"
24
+ spec.add_development_dependency "rake"
25
+ end
@@ -0,0 +1,33 @@
1
+ module HttpspecSimple
2
+ RSpec::Matchers.define :be_http_ok do
3
+ match do |actual|
4
+ actual.status == '200'
5
+ end
6
+
7
+ failure_message_for_should do |actual|
8
+ "expected: 200\n got: #{actual.status}"
9
+ end
10
+
11
+ failure_message_for_should_not do |actual|
12
+ "#{actual.to_s} would not be http ok"
13
+ end
14
+ end
15
+
16
+ RSpec::Matchers.define :respond_within do |expected|
17
+ match do |actual|
18
+ actual.response_time < expected
19
+ end
20
+
21
+ failure_message_for_should do |actual|
22
+ "expected: #{"%7.3f" % expected} seconds\n result: #{"%7.3f" % actual.response_time} seconds"
23
+ end
24
+
25
+ define_method :seconds do
26
+ self
27
+ end
28
+
29
+ define_method :description do
30
+ "respond within #{expected} seconds"
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,48 @@
1
+ require 'net/http'
2
+ require 'uri'
3
+
4
+ module HttpspecSimple
5
+ class Request
6
+ attr_reader :response_time, :status
7
+
8
+ def initialize(url, opt = {})
9
+ @url = URI.parse(url)
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
+ @status, @response_time = http.start do |http|
17
+ res, response_time = process_time do
18
+ open_timeout_error = if Net.const_defined?(:OpenTimeout) then Net::OpenTimeout else Timeout::Error end
19
+ read_timeout_error = if Net.const_defined?(:ReadTimeout) then Net::ReadTimeout else Timeout::Error end
20
+ begin
21
+ res = http.request(Net::HTTP::Get.new(@url.path))
22
+ raise RequestError.new if res.kind_of?(Net::HTTPClientError) or res.kind_of?(Net::HTTPServerError)
23
+ rescue open_timeout_error, read_timeout_error, RequestError
24
+ retry if (retry_count-=1) > 0
25
+ end
26
+ res
27
+ end
28
+ unless res.nil?
29
+ [res.code, response_time]
30
+ else
31
+ ['timeout', response_time]
32
+ end
33
+ end
34
+ end
35
+
36
+ def process_time
37
+ start_time = Time.now
38
+ ret = yield
39
+ [ret, Time.now - start_time]
40
+ end
41
+
42
+ def to_s
43
+ @url.to_s
44
+ end
45
+ end
46
+
47
+ class RequestError < StandardError; end
48
+ end
@@ -0,0 +1,3 @@
1
+ module HttpspecSimple
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,14 @@
1
+ require "httpspec_simple/version"
2
+ require "httpspec_simple/request"
3
+ require "httpspec_simple/custom_matchers"
4
+
5
+ RSpec.configure do |config|
6
+ config.add_setting :base_url, :default => 'http://localhost:10080'
7
+ end
8
+
9
+ def base_url url
10
+ RSpec.configuration.base_url = url
11
+ end
12
+ def request path
13
+ HttpspecSimple::Request.new("#{RSpec.configuration.base_url}#{path}")
14
+ end
@@ -0,0 +1,25 @@
1
+ require 'spec_helper'
2
+
3
+ describe 'custom matchers' do
4
+ describe '#be_http_ok' do
5
+ it "should check status code" do
6
+ response = nil
7
+ server_start { response = request('/') }
8
+ expect {
9
+ response.should be_http_ok
10
+ }.not_to raise_error
11
+ end
12
+ end
13
+
14
+ describe '#respond_within(num).seconds' do
15
+ it "should check response time" do
16
+ requests, response = server_start('/' => Proc.new {|req, res| sleep 2 }) do
17
+ request('/')
18
+ end
19
+ response.should respond_within(3).seconds
20
+ expect {
21
+ response.should respond_within(1).seconds
22
+ }.to raise_error(RSpec::Expectations::ExpectationNotMetError)
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,53 @@
1
+ require 'spec_helper'
2
+
3
+ describe HttpspecSimple::Request do
4
+ it "should request the url passed to 1st argument of constructor" do
5
+ requests, response = server_start do
6
+ HttpspecSimple::Request.new('http://localhost:10080/')
7
+ end
8
+ requests.should have(1).items
9
+ requests.should include('http://localhost:10080/')
10
+ response.status.should == "200"
11
+ end
12
+
13
+ describe "initialize with :timeout option" do
14
+ it "should timeout within a specific time" do
15
+ requests, response = server_start( '/' => Proc.new {|req, res| sleep 2 } ) do
16
+ HttpspecSimple::Request.new('http://localhost:10080/', :timeout => 1)
17
+ end
18
+ response.status.should == "timeout"
19
+ end
20
+ end
21
+
22
+ it "should not retry a request by the default option" do
23
+ requests, response = server_start( '/' => Proc.new {|req, res| res.status = "503" } ) do
24
+ HttpspecSimple::Request.new('http://localhost:10080/')
25
+ end
26
+ requests.should have(1).items
27
+ response.status.should == "503"
28
+ end
29
+
30
+ describe "initialize with :retry option" do
31
+ context ":retry is 3, and the server will respond a status ok at 3rd time" do
32
+ it "should retry requests while response is not ok" do
33
+ response_codes = %w{503 404 200}
34
+ requests, response = server_start( '/' => Proc.new {|req, res| res.status = response_codes.shift } ) do
35
+ HttpspecSimple::Request.new('http://localhost:10080/', :retry => 3)
36
+ end
37
+ requests.should have(3).items
38
+ response.status.should == "200"
39
+ end
40
+ end
41
+
42
+ context ":retry is 3, and the server will not respond a status ok" do
43
+ it "should retry requests 3 times" do
44
+ response_codes = %w{503 500 404}
45
+ requests, response = server_start( '/' => Proc.new {|req, res| res.status = response_codes.shift } ) do
46
+ HttpspecSimple::Request.new('http://localhost:10080/', :retry => 3)
47
+ end
48
+ requests.should have(3).items
49
+ response.status.should == "404"
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ describe "RSpec configuration" do
4
+ describe "#base_url" do
5
+ it "should be callable" do
6
+ expect {
7
+ base_url 'http://www.example.com'
8
+ }.not_to raise_error
9
+ end
10
+ end
11
+ describe "#request" do
12
+ it "should be callable" do
13
+ expect {
14
+ server_start { request '/' }
15
+ }.not_to raise_error
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,38 @@
1
+ require 'webrick'
2
+ require 'httpspec_simple'
3
+
4
+ def server_start mount_procs = {}
5
+ unless mount_procs.has_key?('/')
6
+ mount_procs['/'] = Proc.new {|req, res| res.status = "200" }
7
+ end
8
+ captured = []
9
+ server_thread = Thread.new do
10
+ server = WEBrick::HTTPServer.new(
11
+ Port: 10080,
12
+ Logger: WEBrick::Log.new('/dev/null'),
13
+ AccessLog: [],
14
+ StartCallback: Proc.new { Thread.main.wakeup }
15
+ )
16
+ mount_procs.each {|path, proc|
17
+ server.mount_proc(path) {|req, res|
18
+ captured.push(req.request_uri.to_s)
19
+ proc.call(req, res)
20
+ }
21
+ }
22
+ Thread.current[:server] = server
23
+ server.start
24
+ end
25
+ Thread.stop
26
+ begin
27
+ ret_val = yield
28
+ ensure
29
+ server_thread[:server].shutdown
30
+ end
31
+ [captured, ret_val]
32
+ end
33
+
34
+ RSpec.configure do |config|
35
+ config.treat_symbols_as_metadata_keys_with_true_values = true
36
+ config.run_all_when_everything_filtered = true
37
+ config.filter_run :focus
38
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: httpspec_simple
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Koji NAKAMURA
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-07-23 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 2.14.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 2.14.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: RSpec extension for HTTP request
56
+ email:
57
+ - kozy4324@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - httpspec_simple.gemspec
69
+ - lib/httpspec_simple.rb
70
+ - lib/httpspec_simple/custom_matchers.rb
71
+ - lib/httpspec_simple/request.rb
72
+ - lib/httpspec_simple/version.rb
73
+ - spec/httpspec_simple/custom_matcher_spec.rb
74
+ - spec/httpspec_simple/request_spec.rb
75
+ - spec/httpspec_simple_spec.rb
76
+ - spec/spec_helper.rb
77
+ homepage: https://github.com/kozy4324/httpspec_simple
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - '>='
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubyforge_project:
97
+ rubygems_version: 2.0.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: RSpec extension for HTTP request
101
+ test_files:
102
+ - spec/httpspec_simple/custom_matcher_spec.rb
103
+ - spec/httpspec_simple/request_spec.rb
104
+ - spec/httpspec_simple_spec.rb
105
+ - spec/spec_helper.rb