rafaelss-fakepage 0.2.0 → 0.2.1
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.
- data/lib/fakepage.rb +66 -0
- data/test/spec_fakepage.rb +113 -0
- metadata +6 -5
data/lib/fakepage.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'rack/handler/thin'
|
2
|
+
|
3
|
+
class FakePage
|
4
|
+
include Rack
|
5
|
+
|
6
|
+
attr_accessor :url
|
7
|
+
|
8
|
+
def initialize(name, options)
|
9
|
+
@url = "http://localhost:#{@@options[:server_port]}/#{name}"
|
10
|
+
|
11
|
+
@@maps ||= {}
|
12
|
+
@@maps["/#{name}"] = lambda do |env|
|
13
|
+
options[:method] ||= :get
|
14
|
+
|
15
|
+
headers = { 'Content-Type' => options[:content_type] || 'text/plain' }
|
16
|
+
if options[:method].to_s.upcase == env['REQUEST_METHOD']
|
17
|
+
code = options[:code] || 200
|
18
|
+
body = options[:body] || ""
|
19
|
+
else
|
20
|
+
code = 405
|
21
|
+
body = Rack::Utils::HTTP_STATUS_CODES[code]
|
22
|
+
end
|
23
|
+
[ code, headers, [ body ] ]
|
24
|
+
end
|
25
|
+
@@urlmap.remap(@@maps)
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.get(name, options)
|
29
|
+
self.new(name, options.merge(:method => :get))
|
30
|
+
end
|
31
|
+
|
32
|
+
def self.post(name, options)
|
33
|
+
self.new(name, options.merge(:method => :post))
|
34
|
+
end
|
35
|
+
|
36
|
+
def self.put(name, options)
|
37
|
+
self.new(name, options.merge(:method => :put))
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.head(name, options)
|
41
|
+
self.new(name, options.merge(:method => :head))
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.options
|
45
|
+
@@options || {}
|
46
|
+
end
|
47
|
+
|
48
|
+
def self.start(options = {})
|
49
|
+
@@options = { :server_port => 9292 }.merge(options)
|
50
|
+
|
51
|
+
@@urlmap = URLMap.new
|
52
|
+
@@thread = Thread.new do
|
53
|
+
Thin::Logging.silent = true
|
54
|
+
Handler::Thin.run(Lint.new(@@urlmap), :Host => '0.0.0.0', :Port => @@options[:server_port])
|
55
|
+
end
|
56
|
+
at_exit do
|
57
|
+
@@thread.kill
|
58
|
+
end
|
59
|
+
|
60
|
+
sleep(1)
|
61
|
+
end
|
62
|
+
|
63
|
+
def self.stop
|
64
|
+
@@thread.kill
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require 'curb'
|
2
|
+
require File.expand_path('../../lib/fakepage', __FILE__)
|
3
|
+
|
4
|
+
FakePage.start
|
5
|
+
|
6
|
+
def request(name, options = {})
|
7
|
+
page = FakePage.new(name, { :code => 200 }.merge(options))
|
8
|
+
Curl::Easy.perform(page.url)
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "FakePage" do
|
12
|
+
|
13
|
+
it "should have methods for get, post, put and head http methods" do
|
14
|
+
FakePage.respond_to?(:get).should.be.true
|
15
|
+
FakePage.respond_to?(:post).should.be.true
|
16
|
+
FakePage.respond_to?(:put).should.be.true
|
17
|
+
FakePage.respond_to?(:head).should.be.true
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "performing requests" do
|
21
|
+
|
22
|
+
allowed_methods = [:get, :post, :put, :head]
|
23
|
+
|
24
|
+
allowed_methods.each do |method|
|
25
|
+
|
26
|
+
describe method.to_s.upcase do
|
27
|
+
|
28
|
+
before do
|
29
|
+
@page = FakePage.new("do_#{method}", :method => method)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "should returns status code 200 for #{method}" do
|
33
|
+
response = Curl::Easy.perform(@page.url) do |curl|
|
34
|
+
if method == :put
|
35
|
+
curl.send(:"http_#{method}", "data")
|
36
|
+
else
|
37
|
+
curl.send(:"http_#{method}")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
response.response_code.should.be.equal 200
|
42
|
+
end
|
43
|
+
|
44
|
+
allowed_methods.select { |m| m != method }.each do |not_allowed_method|
|
45
|
+
|
46
|
+
it "should returns status code 405 for #{not_allowed_method}" do
|
47
|
+
response = Curl::Easy.perform(@page.url) do |curl|
|
48
|
+
if not_allowed_method == :put
|
49
|
+
curl.send(:"http_#{not_allowed_method}", "data")
|
50
|
+
else
|
51
|
+
curl.send(:"http_#{not_allowed_method}")
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
response.response_code.should.be.equal 405
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "server configurations" do
|
63
|
+
|
64
|
+
it "should have a default port" do
|
65
|
+
FakePage.options[:server_port].should.be.equal 9292
|
66
|
+
end
|
67
|
+
|
68
|
+
it "should change the default port" do
|
69
|
+
FakePage.stop
|
70
|
+
|
71
|
+
FakePage.start(:server_port => 9876)
|
72
|
+
response = request("default_port_change")
|
73
|
+
response.url.should.include "9876"
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "options" do
|
78
|
+
|
79
|
+
describe "defaults" do
|
80
|
+
it "should define empty for nil :body" do
|
81
|
+
response = request("nil_body")
|
82
|
+
response.body_str.should.be.empty
|
83
|
+
end
|
84
|
+
|
85
|
+
it "should define 200 for nil :code" do
|
86
|
+
response = request("nil_status_code")
|
87
|
+
response.response_code.should.be.equal 200
|
88
|
+
end
|
89
|
+
|
90
|
+
it "should define 'text/plain' for nil :content_type" do
|
91
|
+
response = request("nil_content_type")
|
92
|
+
response.content_type.should.be.equal "text/plain"
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "custom" do
|
97
|
+
it "should accept :body" do
|
98
|
+
response = request("body", :body => "body of my page")
|
99
|
+
response.body_str.should.be.equal "body of my page"
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should accept :code for status code" do
|
103
|
+
response = request("status_code", :code => 404)
|
104
|
+
response.response_code.should.be.equal 404
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should accept :content_type" do
|
108
|
+
response = request("content_type", :content_type => "text/html")
|
109
|
+
response.content_type.should.be.equal "text/html"
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rafaelss-fakepage
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rafael Souza
|
@@ -30,8 +30,9 @@ extensions: []
|
|
30
30
|
|
31
31
|
extra_rdoc_files: []
|
32
32
|
|
33
|
-
files:
|
34
|
-
|
33
|
+
files:
|
34
|
+
- lib/fakepage.rb
|
35
|
+
- test/spec_fakepage.rb
|
35
36
|
has_rdoc: false
|
36
37
|
homepage: http://rafaelss.com/
|
37
38
|
licenses:
|
@@ -59,5 +60,5 @@ rubygems_version: 1.3.5
|
|
59
60
|
signing_key:
|
60
61
|
specification_version: 3
|
61
62
|
summary: Agnostic library for test web requests creating fake pages
|
62
|
-
test_files:
|
63
|
-
|
63
|
+
test_files:
|
64
|
+
- test/spec_fakepage.rb
|