webmock-twirp 0.1.0 → 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +58 -0
- data/lib/webmock/twirp/request_stub.rb +3 -2
- data/lib/webmock/twirp/version.rb +1 -1
- data/lib/webmock/twirp.rb +1 -2
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 944a35bac7c3e752f543bc37f6d2b0a66a1a7f771022ac28c9c30ddba2e92891
|
4
|
+
data.tar.gz: db6bfad348186e05d483d6d09c485fd470679e3978fcca131e93d5fcee6649de
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b7fb99d4bf12cad35620ef9bb22bd7faabd626254c6e5a307b27da7478091eaf88256d0fc902cb507311312837c52ecc0b238796cedc0153674dbb34db69308
|
7
|
+
data.tar.gz: 8f4bf9c6f3b828843e2cc8167c4b47e7ccda55a56dda1c678f5b1cf7e2b0eba01207e71572d08e13004e7c031f5ef950160f61a907ffffefbbec862be7d1918b
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -2,7 +2,12 @@ WebMock::Twirp
|
|
2
2
|
======
|
3
3
|
Twirp support for [WebMock](https://github.com/bblimke/webmock). All our favorite http request stubbing for Twirp RPCs - message and error serialization done automatically.
|
4
4
|
|
5
|
+
### Install
|
6
|
+
```ruby
|
7
|
+
gem "webmock-twirp"
|
8
|
+
```
|
5
9
|
|
10
|
+
### Example
|
6
11
|
```ruby
|
7
12
|
require "webmock/twirp"
|
8
13
|
|
@@ -37,6 +42,59 @@ end
|
|
37
42
|
```
|
38
43
|
|
39
44
|
|
45
|
+
## Usage
|
46
|
+
|
47
|
+
### .with
|
48
|
+
`stub_twirp_request.with` allows you to only stub requests which match specific attributes. It accepts a hash or a `Google::Protobuf::MessageExts` instance. The hash supports constants, regexes, and rspec matchers.
|
49
|
+
|
50
|
+
```ruby
|
51
|
+
stub_twirp_request.with(message: "hi")
|
52
|
+
stub_twirp_request.with(message: /^h/)
|
53
|
+
stub_twirp_request.with(message: include("i"))
|
54
|
+
|
55
|
+
expected_request = MyTwirpRequest.new(message: "hi")
|
56
|
+
stub_twirp_request.with(expected_request)
|
57
|
+
```
|
58
|
+
|
59
|
+
|
60
|
+
If you want even more control over the matching criteria, use the block mode. A `Protobuf` instance is passed into the block with the request's parameters.
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
stub_twirp_request.with do |request|
|
64
|
+
request.message == "hi"
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
|
69
|
+
### .to_return
|
70
|
+
`stub_twirp_request.to_return` allows you to specify a response, or use a default response. It can be a hash or `Protobuf` instance. To return an error, specify an error code, http status, or `Twirp::Error`.
|
71
|
+
|
72
|
+
```ruby
|
73
|
+
stub_twirp_request.to_return # ie. `MyTwirpResponse.new`
|
74
|
+
|
75
|
+
stub_twirp_request.to_return(msg: "bye")
|
76
|
+
|
77
|
+
response = MyTwirpResponse.new(msg: "bye")
|
78
|
+
stub_twirp_request.to_return(response)
|
79
|
+
|
80
|
+
# errors
|
81
|
+
stub_twirp_request.to_return(:not_found)
|
82
|
+
stub_twirp_request.to_return(404)
|
83
|
+
stub_twirp_request.to_return(Twirp::Error.not_found("Nope"))
|
84
|
+
```
|
85
|
+
|
86
|
+
The block mode passes in the request Protobuf.
|
87
|
+
```ruby
|
88
|
+
stub_twirp_request.to_return do |request|
|
89
|
+
if request.message == "hi"
|
90
|
+
{ msg: "bye" }
|
91
|
+
else
|
92
|
+
:not_found
|
93
|
+
end
|
94
|
+
end
|
95
|
+
```
|
96
|
+
|
97
|
+
|
40
98
|
----
|
41
99
|
## Contributing
|
42
100
|
|
@@ -117,6 +117,7 @@ module WebMock
|
|
117
117
|
|
118
118
|
super(*response_hashes, &decoder)
|
119
119
|
end
|
120
|
+
alias_method :and_return, :to_return # update existing alias
|
120
121
|
|
121
122
|
def to_return_json(*)
|
122
123
|
raise NotImplementedError
|
@@ -168,10 +169,10 @@ module WebMock
|
|
168
169
|
if code = ::Twirp::ERROR_CODES_TO_HTTP_STATUS.key(obj)
|
169
170
|
::Twirp::Error.new(code, code)
|
170
171
|
else
|
171
|
-
raise ArgumentError, "invalid error
|
172
|
+
raise ArgumentError, "invalid http error status: #{obj}"
|
172
173
|
end
|
173
174
|
else
|
174
|
-
raise
|
175
|
+
raise ArgumentError, "can not generate twirp reponse from: #{obj}"
|
175
176
|
end
|
176
177
|
|
177
178
|
if res.is_a?(Google::Protobuf::MessageExts)
|
data/lib/webmock/twirp.rb
CHANGED
@@ -31,8 +31,7 @@ if $LOADED_FEATURES.find { |x| x =~ %r{/webmock/rspec.rb} }
|
|
31
31
|
# require "webmock/rspec"
|
32
32
|
RSpec.configure { |c| c.include WebMock::Twirp::API }
|
33
33
|
else
|
34
|
-
# patch
|
35
|
-
|
34
|
+
# patch WebMock to also export stub_twirp_request
|
36
35
|
module WebMock
|
37
36
|
module API
|
38
37
|
include WebMock::Twirp::API
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webmock-twirp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Pepper
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-10-
|
11
|
+
date: 2022-10-11 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: webmock
|