webstub 0.3.0 → 0.3.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/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ .rvmrc
1
2
  .repl_history
2
3
  build
3
4
  resources/*.nib
data/README.md ADDED
@@ -0,0 +1,118 @@
1
+ WebStub [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/mattgreen/webstub)
2
+ ======
3
+
4
+ What if [WebMock](https://github.com/bblimke/webmock) and [NSURLProtocol](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSURLProtocol_Class/Reference/Reference.html) had a baby?
5
+
6
+ Features
7
+ ------------
8
+ * Supports most any HTTP library that is built on NSURLConnection
9
+ * Request matching based upon HTTP method, URI, and body
10
+ * Optionally, disable real network access
11
+ * Familiar, delicious syntax
12
+ * Bacon integration
13
+
14
+ Installation
15
+ ------------
16
+ Please ensure you have the latest version of RubyMotion: (**WebStub requres version 1.24 or higher**)
17
+
18
+ $ sudo motion update
19
+
20
+ Also, if you haven't done so already, please [configure](http://thunderboltlabs.com/posts/using-bundler-with-rubymotion) your project to use Bundler.
21
+
22
+ Update your Gemfile:
23
+
24
+ gem "webstub", "~> 0.3.0"
25
+
26
+ Bundle:
27
+
28
+ $ bundle install
29
+
30
+ Usage
31
+ -----
32
+ * Add the following line to the top-most `describe` block in your spec:
33
+
34
+ `extend WebStub::SpecHelpers`
35
+
36
+ * Use the following methods to control the use of request stubbing:
37
+ - `disable_network_access!`
38
+ - `enable_network_access!`
39
+ - `stub_request`
40
+ - `reset_stubs`
41
+
42
+ Example Spec
43
+ ------------
44
+
45
+ ```ruby
46
+ describe "Example" do
47
+ extend WebStub::SpecHelpers
48
+
49
+ describe "Stubbing a GET request to return a simple response" do
50
+ it "retrieves the front page" do
51
+ stub_request(:get, "http://example.com/").
52
+ to_return(body: "Hello!", content_type: "text/plain")
53
+
54
+ @body = nil
55
+ @api.get_index do |body, error|
56
+ @body = body
57
+ resume
58
+ end
59
+
60
+ wait_max 1.0 do
61
+ @body.should.be == "Hello!"
62
+ end
63
+ end
64
+ end
65
+
66
+ describe "Stubbing a GET request to return JSON" do
67
+ it "retrieves suggestions" do
68
+ stub_request(:get, "https://example.com/suggestions?q=mu").
69
+ to_return(json: { suggestions: ["muse"] })
70
+
71
+ @suggestions = nil
72
+ @api.get_suggestions("mu") do |results, error|
73
+ @suggestions = results
74
+ resume
75
+ end
76
+
77
+ wait_max 1.0 do
78
+ @suggestions.should.not.be.empty
79
+ end
80
+ end
81
+ end
82
+
83
+ describe "Stubbing a POST request to return JSON" do
84
+ it "handles a POST request" do
85
+ stub_request(:post, "https://example.com/action").
86
+ with(body: { q: "unsustainable" }).
87
+ to_return(json: [ { album: "The 2nd Law", release_date: "2012-10-01", artist: "Muse" } ])
88
+
89
+ @results = nil
90
+ @api.get_album_info_for_track("unsustainable") do |results, error|
91
+ @results = results
92
+ resume
93
+ end
94
+
95
+ wait_max 1.0 do
96
+ @results.should.not.be.empty
97
+ end
98
+ end
99
+ end
100
+ end
101
+ ```
102
+
103
+ Conventions
104
+ -----------------
105
+ - The URL is matched *exactly* as is right now (hence query parameters need to be included and encoded)
106
+ - The `with` method's `body` option accepts either a Hash or a String:
107
+ - Hashes are assumed to be form data (with a `application/x-www-form-urlencoded` content type)
108
+ - Strings are matched as is
109
+ - The `to_return` method accepts a few options:
110
+ - `json`: accepts either a Hash or a String. If a Hash is provided, it will be converted to JSON. Strings are returned as is, with the Content-Type set to `application/json`.
111
+ - `body`: accepts a String, and returns it as-is
112
+ - `content_type`: sets the Content-Type when using the `body` parameter
113
+
114
+ TODO
115
+ ---------
116
+ * Handle query params similarly to form data
117
+ * URI canonicalization
118
+
@@ -0,0 +1,9 @@
1
+ module WebStub
2
+ module SpecHelpers
3
+ def self.extended(base)
4
+ base.class.send(:include, WebStub::API)
5
+
6
+ base.after { reset_stubs }
7
+ end
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module WebStub
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -0,0 +1,11 @@
1
+ describe WebStub::SpecHelpers do
2
+ extend WebStub::SpecHelpers
3
+
4
+ it "includes WebStub::API" do
5
+ self.class.ancestors.should.include? WebStub::API
6
+ end
7
+
8
+ it "calls reset_stubs in an after block" do
9
+ self.instance_variable_get(:@after).should.not.be.empty
10
+ end
11
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: webstub
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-09-18 00:00:00.000000000 Z
12
+ date: 2012-09-22 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Easily stub out HTTP responses in RubyMotion specs
15
15
  email:
@@ -19,19 +19,19 @@ extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
21
  - .gitignore
22
- - .rvmrc
23
22
  - Gemfile
24
23
  - Gemfile.lock
25
24
  - Guardfile
26
25
  - LICENSE
26
+ - README.md
27
27
  - Rakefile
28
28
  - lib/spec/spec_delegate.rb
29
29
  - lib/webstub.rb
30
30
  - lib/webstub/api.rb
31
- - lib/webstub/bacon.rb
32
31
  - lib/webstub/json.rb
33
32
  - lib/webstub/protocol.rb
34
33
  - lib/webstub/registry.rb
34
+ - lib/webstub/spec_helpers.rb
35
35
  - lib/webstub/stub.rb
36
36
  - lib/webstub/uri.rb
37
37
  - lib/webstub/version.rb
@@ -40,6 +40,7 @@ files:
40
40
  - spec/json_spec.rb
41
41
  - spec/protocol_spec.rb
42
42
  - spec/registry_spec.rb
43
+ - spec/spec_helpers_spec.rb
43
44
  - spec/stub_spec.rb
44
45
  - spec/uri_spec.rb
45
46
  - webstub.gemspec
@@ -73,6 +74,7 @@ test_files:
73
74
  - spec/json_spec.rb
74
75
  - spec/protocol_spec.rb
75
76
  - spec/registry_spec.rb
77
+ - spec/spec_helpers_spec.rb
76
78
  - spec/stub_spec.rb
77
79
  - spec/uri_spec.rb
78
80
  has_rdoc:
data/.rvmrc DELETED
@@ -1,5 +0,0 @@
1
- rvm ruby-1.9.3-p194@rubymotion --create
2
-
3
- [[ -s "config/rvm_env.sh" ]] && . "config/rvm_env.sh"
4
- [[ -s ".rvm_env" ]] && . ".rvm_env"
5
-
data/lib/webstub/bacon.rb DELETED
@@ -1,7 +0,0 @@
1
- # HACK: need define_method to be legal
2
- class Bacon
3
- class Context
4
- include WebStub::API
5
- end
6
- end
7
-