soaspec 0.2.23 → 0.2.24
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 +4 -4
- data/.gitignore +15 -15
- data/.gitlab-ci.yml +33 -33
- data/.rspec +3 -3
- data/.rubocop.yml +2 -2
- data/CODE_OF_CONDUCT.md +74 -74
- data/ChangeLog +577 -573
- data/Gemfile +6 -6
- data/LICENSE.txt +21 -21
- data/README.md +230 -230
- data/Rakefile +42 -42
- data/Todo.md +15 -15
- data/exe/soaspec +123 -123
- data/exe/xml_to_yaml_file +42 -42
- data/lib/soaspec.rb +101 -101
- data/lib/soaspec/core_ext/hash.rb +35 -35
- data/lib/soaspec/cucumber/generic_steps.rb +85 -85
- data/lib/soaspec/demo.rb +4 -4
- data/lib/soaspec/exchange/exchange.rb +111 -111
- data/lib/soaspec/exchange/exchange_extractor.rb +83 -83
- data/lib/soaspec/exchange/exchange_properties.rb +26 -26
- data/lib/soaspec/exchange/exchange_repeater.rb +19 -19
- data/lib/soaspec/exchange/request_builder.rb +68 -68
- data/lib/soaspec/exchange/variable_storer.rb +22 -22
- data/lib/soaspec/exchange_handlers/exchange_handler.rb +126 -126
- data/lib/soaspec/exchange_handlers/handler_accessors.rb +130 -130
- data/lib/soaspec/exchange_handlers/response_extractor.rb +82 -82
- data/lib/soaspec/exchange_handlers/rest_exchanger_factory.rb +109 -109
- data/lib/soaspec/exchange_handlers/rest_handler.rb +259 -259
- data/lib/soaspec/exchange_handlers/rest_methods.rb +44 -44
- data/lib/soaspec/exchange_handlers/rest_parameters.rb +86 -86
- data/lib/soaspec/exchange_handlers/rest_parameters_defaults.rb +21 -21
- data/lib/soaspec/exchange_handlers/soap_handler.rb +235 -235
- data/lib/soaspec/exe_helpers.rb +92 -92
- data/lib/soaspec/generate_server.rb +37 -37
- data/lib/soaspec/generator/.rspec.erb +5 -5
- data/lib/soaspec/generator/.travis.yml.erb +5 -5
- data/lib/soaspec/generator/Gemfile.erb +8 -8
- data/lib/soaspec/generator/README.md.erb +29 -29
- data/lib/soaspec/generator/Rakefile.erb +19 -19
- data/lib/soaspec/generator/config/data/default.yml.erb +2 -2
- data/lib/soaspec/generator/css/bootstrap.css +6833 -6833
- data/lib/soaspec/generator/generate_exchange.html.erb +35 -35
- data/lib/soaspec/generator/lib/blz_service.rb.erb +26 -26
- data/lib/soaspec/generator/lib/dynamic_class_content.rb.erb +12 -12
- data/lib/soaspec/generator/lib/new_rest_service.rb.erb +51 -51
- data/lib/soaspec/generator/lib/new_soap_service.rb.erb +29 -29
- data/lib/soaspec/generator/lib/package_service.rb.erb +2 -2
- data/lib/soaspec/generator/lib/shared_example.rb.erb +8 -8
- data/lib/soaspec/generator/spec/dynamic_soap_spec.rb.erb +12 -12
- data/lib/soaspec/generator/spec/rest_spec.rb.erb +9 -9
- data/lib/soaspec/generator/spec/soap_spec.rb.erb +51 -51
- data/lib/soaspec/generator/spec/spec_helper.rb.erb +23 -23
- data/lib/soaspec/generator/template/soap_template.xml +6 -6
- data/lib/soaspec/indifferent_hash.rb +7 -7
- data/lib/soaspec/interpreter.rb +39 -39
- data/lib/soaspec/matchers.rb +114 -114
- data/lib/soaspec/not_found_errors.rb +13 -13
- data/lib/soaspec/o_auth2.rb +128 -128
- data/lib/soaspec/soaspec_shared_examples.rb +24 -24
- data/lib/soaspec/spec_logger.rb +121 -121
- data/lib/soaspec/template_reader.rb +28 -28
- data/lib/soaspec/test_server/bank.wsdl +90 -90
- data/lib/soaspec/test_server/get_bank.rb +164 -164
- data/lib/soaspec/test_server/id_manager.rb +39 -39
- data/lib/soaspec/test_server/invoices.rb +27 -27
- data/lib/soaspec/test_server/namespace.xml +14 -14
- data/lib/soaspec/test_server/note.xml +5 -5
- data/lib/soaspec/test_server/puppy_service.rb +19 -19
- data/lib/soaspec/test_server/test_attribute.rb +12 -12
- data/lib/soaspec/test_server/test_namespace.rb +12 -12
- data/lib/soaspec/version.rb +4 -3
- data/lib/soaspec/virtual_server.rb +174 -174
- data/lib/soaspec/wait.rb +41 -41
- data/lib/soaspec/wsdl_generator.rb +215 -215
- data/soaspec.gemspec +53 -53
- data/test.wsdl +116 -116
- data/test.xml +10 -10
- data/test_wsdl.rb +41 -41
- metadata +3 -4
data/Gemfile
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
source 'https://rubygems.org'
|
2
|
-
|
3
|
-
git_source(:gitlab) { |_repo_name| 'https://gitlab.com/samuel-garratt/soaspec' }
|
4
|
-
|
5
|
-
# Specify your gem's dependencies in soaspec.gemspec
|
6
|
-
gemspec
|
1
|
+
source 'https://rubygems.org'
|
2
|
+
|
3
|
+
git_source(:gitlab) { |_repo_name| 'https://gitlab.com/samuel-garratt/soaspec' }
|
4
|
+
|
5
|
+
# Specify your gem's dependencies in soaspec.gemspec
|
6
|
+
gemspec
|
data/LICENSE.txt
CHANGED
@@ -1,21 +1,21 @@
|
|
1
|
-
The MIT License (MIT)
|
2
|
-
|
3
|
-
Copyright (c) 2018 Samuel Garratt
|
4
|
-
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
7
|
-
in the Software without restriction, including without limitation the rights
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
10
|
-
furnished to do so, subject to the following conditions:
|
11
|
-
|
12
|
-
The above copyright notice and this permission notice shall be included in
|
13
|
-
all copies or substantial portions of the Software.
|
14
|
-
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
-
THE SOFTWARE.
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Samuel Garratt
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
CHANGED
@@ -1,230 +1,230 @@
|
|
1
|
-
# Soaspec
|
2
|
-
|
3
|
-
This gem helps to represent multiple API tests against a backend briefly, concisely and clearly.
|
4
|
-
|
5
|
-

|
6
|
-
> Example showing how a REST 'get' call can be made and it's response extracted.
|
7
|
-
|
8
|
-
[](https://gitlab.com/samuel-garratt/soaspec/pipelines)
|
9
|
-
[](https://samuel-garratt.gitlab.io/soaspec/coverage)
|
10
|
-
|
11
|
-
It is essentially a wrapper around the Savon and RestClient gems, adding useful functionality including
|
12
|
-
|
13
|
-
* Creating multiple API calls from the same base configuration through the use of an `ExchangeHandler` class
|
14
|
-
* Extracting values from response body's through either `XPath` or `JSONPath`
|
15
|
-
* Building up a custom RSpec `success scenario` shared example to reuse common tests on an API
|
16
|
-
* Methods simplifying setting and extracting values from a `Request/Response` pair (`Exchange`)
|
17
|
-
* Waiting for a particular response from an API by polling it
|
18
|
-
* Representing paths to values from a response with business-meaningful method names
|
19
|
-
* Generating initial code for testing an API with `soaspec new`
|
20
|
-
* Accessing and utilising `oauth2` access tokens
|
21
|
-
* Hosting a `virtual_server` that simulates REST & SOAP responses from an API
|
22
|
-
|
23
|
-
## Installation
|
24
|
-
|
25
|
-
Add this line to your application's Gemfile:
|
26
|
-
|
27
|
-
```ruby
|
28
|
-
gem 'soaspec'
|
29
|
-
```
|
30
|
-
|
31
|
-
And then execute:
|
32
|
-
|
33
|
-
$ bundle
|
34
|
-
|
35
|
-
Or install it yourself as:
|
36
|
-
|
37
|
-
$ gem install soaspec
|
38
|
-
|
39
|
-
[Things to be done](Todo.md)
|
40
|
-
|
41
|
-
## Getting Started
|
42
|
-
|
43
|
-
To create a new test suite using this you can use the `soaspec` binary.
|
44
|
-
|
45
|
-
Example:
|
46
|
-
|
47
|
-
```
|
48
|
-
mkdir 'api_test'
|
49
|
-
cd 'api_test'
|
50
|
-
soaspec new [rest/soap]
|
51
|
-
bundle install
|
52
|
-
```
|
53
|
-
|
54
|
-
Then you can run the tests with:
|
55
|
-
|
56
|
-
```
|
57
|
-
rake spec
|
58
|
-
```
|
59
|
-
|
60
|
-
You can also use `soaspec generate` to generate a set of tests from a WSDL. This is still in trial period and will be finished probably after Savon 3 is more stable.
|
61
|
-
|
62
|
-
## Usage
|
63
|
-
|
64
|
-
* SOAP - this uses Savon behind the scenes. Some defaults are overridden. Please see 'soap_handler.rb'-'default_options' method
|
65
|
-
for such defaults. When describing an API override this in 'savon_options' method
|
66
|
-
* REST - this uses the resource class from the Rest-Client gem behind the scenes.
|
67
|
-
|
68
|
-
See [spec](spec) and [features](features) for example of usage.
|
69
|
-
|
70
|
-
### ExchangeHandler
|
71
|
-
|
72
|
-
To start with, create a class inheriting from a ‘Handler’ class for each web service that needs testing.
|
73
|
-
In this class you define the common parameters used for testing it.
|
74
|
-
|
75
|
-
For example:
|
76
|
-
|
77
|
-
```ruby
|
78
|
-
# Classes are set up through inheriting from either `Soaspec::RestHandler` or `Soaspec::SoapHandler`
|
79
|
-
class PuppyService < Soaspec::RestHandler
|
80
|
-
# Set default headers for all `Exchanges` using this class
|
81
|
-
headers accept: 'application/json', content_type: 'application/json'
|
82
|
-
|
83
|
-
# URL for which all requests using this class will start with
|
84
|
-
base_url 'http://petstore.swagger.io/v2/pet'
|
85
|
-
|
86
|
-
# Accessing parts of a response
|
87
|
-
|
88
|
-
# Define a method 'id' that can be obtained with either XPATH '//id' or JSONPath '$..id'
|
89
|
-
element :id, :id
|
90
|
-
# Define method to obtain a category id through JSON Path
|
91
|
-
element :category_id, '$..category.id'
|
92
|
-
end
|
93
|
-
```
|
94
|
-
|
95
|
-
> You can easily create a exchange handler with the `soaspec add` command. This will also add comments explaining common methods that can be used
|
96
|
-
|
97
|
-
### Exchange
|
98
|
-
|
99
|
-
After creating the `ExchangeHandler`, you reference this class in creating `Exchange`s (objects that each represent a request / response pair).
|
100
|
-
Upon initialization of the Exchange object (or later on through setters), parameters specific to this request are set.
|
101
|
-
Most getters of the `Exchange` are on the response & will implicitly trigger the API request to be made.
|
102
|
-
Once this request has been made, all following accessors of the response will just use the response of the previous request made.
|
103
|
-
|
104
|
-
For example, to create a http post using the above `ExchangeHandler` and get parts of it's response.
|
105
|
-
```ruby
|
106
|
-
# Create a new Exchange that will post to 'http://petstore.swagger.io/v2/pet' with JSON { "status": "sold" }
|
107
|
-
# The 'body' key will convert it's value from a Hash to JSON
|
108
|
-
exchange = PuppyService.post(body: { status: 'sold' })
|
109
|
-
|
110
|
-
# This will trigger the request to be made & return the response, in this case a RestClient::Response object
|
111
|
-
response = exchange.response
|
112
|
-
# This will reuse the response already received return a value at JSON path $..category.id, throwing an exception if not found
|
113
|
-
exchange.category_id
|
114
|
-
# This will do the same but using path directly in code
|
115
|
-
exchange['$..category.id']
|
116
|
-
# Get the HTTP status code of the response
|
117
|
-
exchange.status_code
|
118
|
-
```
|
119
|
-
|
120
|
-
See [Request Body Parameters](https://gitlab.com/samuel-garratt/soaspec/wikis/RequestBodyParameters) for more details on setting a request body.
|
121
|
-
See [Creating an Exchange](https://gitlab.com/samuel-garratt/soaspec/wikis/CreatingExchange) for details on how to create an Exchange.
|
122
|
-
|
123
|
-
### Virtual Server
|
124
|
-
|
125
|
-
Soaspec includes a virtual server that returns REST and SOAP responses that can be used when learning or experimenting with API testing.
|
126
|
-
The gem itself uses this to test it's own functionality.
|
127
|
-
|
128
|
-
To start the server, after installing the gem, type
|
129
|
-
|
130
|
-
```
|
131
|
-
soaspec virtual_server [port_num]
|
132
|
-
```
|
133
|
-
|
134
|
-
By default it runs on port 4999. This will be used if `port_num` is empty.
|
135
|
-
|
136
|
-
You can look at the documentation for the web services provided at the rool url (e.g `localhost:4999`).
|
137
|
-
|
138
|
-
### RSpec
|
139
|
-
|
140
|
-
For example:
|
141
|
-
|
142
|
-
```ruby
|
143
|
-
context PuppyService.new('Order Puppies') do
|
144
|
-
describe post(:create_pet, body: { status: 'sold' }) do # Post with status as sold in request
|
145
|
-
its(['status']) { is_expected.to eq 'sold' } # Check responses status is sold
|
146
|
-
end
|
147
|
-
end
|
148
|
-
```
|
149
|
-
|
150
|
-
#### Tips
|
151
|
-
|
152
|
-
If you find having a large backtrace on errors or RSpec shared examples such as 'success scenarios' this can shorten the
|
153
|
-
backtrace.
|
154
|
-
|
155
|
-
RSpec.configure do |config|
|
156
|
-
|
157
|
-
config.backtrace_exclusion_patterns = [
|
158
|
-
/rspec/
|
159
|
-
]
|
160
|
-
end
|
161
|
-
|
162
|
-
### Cucumber
|
163
|
-
|
164
|
-
If you're using `Cucumber` then I would recommend the following
|
165
|
-
|
166
|
-
In the `Given` (or background) specify the `Exchange` object.
|
167
|
-
Either store this as an instance variable (e.g `@exchange`) or use the global `Soaspec.last_exchange` (which is automatically set).
|
168
|
-
|
169
|
-
In the `When`, use the `call` method to make the request `@exchange.call`. If problems occur in making the request this should separate such failures from issues with the response.
|
170
|
-
|
171
|
-
In the `Then`, make the assertions from the `@exchange` object.
|
172
|
-
E.g
|
173
|
-
```ruby
|
174
|
-
expect(@exchange['message']).to include 'success'
|
175
|
-
expect(@exchange.status_code).to eq 200
|
176
|
-
```
|
177
|
-
|
178
|
-
### Authentication
|
179
|
-
|
180
|
-
See [the wiki](https://gitlab.com/samuel-garratt/soaspec/wikis/Authentication) for details.
|
181
|
-
|
182
|
-
## Logging
|
183
|
-
|
184
|
-
By default traffic is logged to a file in the `logs` folder called `traffic_DATETIME.log`.
|
185
|
-
|
186
|
-
The `SpecLogger` class is responsible for handling all the logs.
|
187
|
-
|
188
|
-
To change the output file location to 'custom.log':
|
189
|
-
```ruby
|
190
|
-
Soaspec::SpecLogger.traffic_file = 'custom.log'
|
191
|
-
```
|
192
|
-
|
193
|
-
To change the folder the default log is:
|
194
|
-
|
195
|
-
```ruby
|
196
|
-
Soaspec::SpecLogger.traffic_folder = 'custom_folder'
|
197
|
-
```
|
198
|
-
|
199
|
-
To disable logging to a file set the 'output_to_file' to 'false'.
|
200
|
-
```ruby
|
201
|
-
Soaspec::SpecLogger.output_to_file = false
|
202
|
-
```
|
203
|
-
There is a similar parameter to logging to the terminal, STDOUT (by default false).
|
204
|
-
You can also change the color of this output with the `terminal_color` attribute.
|
205
|
-
```ruby
|
206
|
-
Soaspec::SpecLogger.output_to_terminal = true
|
207
|
-
Soaspec::SpecLogger.terminal_color = :magenta
|
208
|
-
```
|
209
|
-
|
210
|
-
## Learning more
|
211
|
-
|
212
|
-
Looking at the website [here](https://samuel-garratt.gitlab.io/soaspec/)
|
213
|
-
|
214
|
-
## Development
|
215
|
-
|
216
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
217
|
-
|
218
|
-
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
219
|
-
|
220
|
-
## Contributing
|
221
|
-
|
222
|
-
Bug reports and pull requests are welcome on GitLab at https://gitlab.com/samuel-garratt/soaspec. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
223
|
-
|
224
|
-
## License
|
225
|
-
|
226
|
-
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
227
|
-
|
228
|
-
## Code of Conduct
|
229
|
-
|
230
|
-
Everyone interacting in the Soaspec project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://gitlab.com/samuel-garratt/soaspec/blob/master/CODE_OF_CONDUCT.md).
|
1
|
+
# Soaspec
|
2
|
+
|
3
|
+
This gem helps to represent multiple API tests against a backend briefly, concisely and clearly.
|
4
|
+
|
5
|
+

|
6
|
+
> Example showing how a REST 'get' call can be made and it's response extracted.
|
7
|
+
|
8
|
+
[](https://gitlab.com/samuel-garratt/soaspec/pipelines)
|
9
|
+
[](https://samuel-garratt.gitlab.io/soaspec/coverage)
|
10
|
+
|
11
|
+
It is essentially a wrapper around the Savon and RestClient gems, adding useful functionality including
|
12
|
+
|
13
|
+
* Creating multiple API calls from the same base configuration through the use of an `ExchangeHandler` class
|
14
|
+
* Extracting values from response body's through either `XPath` or `JSONPath`
|
15
|
+
* Building up a custom RSpec `success scenario` shared example to reuse common tests on an API
|
16
|
+
* Methods simplifying setting and extracting values from a `Request/Response` pair (`Exchange`)
|
17
|
+
* Waiting for a particular response from an API by polling it
|
18
|
+
* Representing paths to values from a response with business-meaningful method names
|
19
|
+
* Generating initial code for testing an API with `soaspec new`
|
20
|
+
* Accessing and utilising `oauth2` access tokens
|
21
|
+
* Hosting a `virtual_server` that simulates REST & SOAP responses from an API
|
22
|
+
|
23
|
+
## Installation
|
24
|
+
|
25
|
+
Add this line to your application's Gemfile:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem 'soaspec'
|
29
|
+
```
|
30
|
+
|
31
|
+
And then execute:
|
32
|
+
|
33
|
+
$ bundle
|
34
|
+
|
35
|
+
Or install it yourself as:
|
36
|
+
|
37
|
+
$ gem install soaspec
|
38
|
+
|
39
|
+
[Things to be done](Todo.md)
|
40
|
+
|
41
|
+
## Getting Started
|
42
|
+
|
43
|
+
To create a new test suite using this you can use the `soaspec` binary.
|
44
|
+
|
45
|
+
Example:
|
46
|
+
|
47
|
+
```
|
48
|
+
mkdir 'api_test'
|
49
|
+
cd 'api_test'
|
50
|
+
soaspec new [rest/soap]
|
51
|
+
bundle install
|
52
|
+
```
|
53
|
+
|
54
|
+
Then you can run the tests with:
|
55
|
+
|
56
|
+
```
|
57
|
+
rake spec
|
58
|
+
```
|
59
|
+
|
60
|
+
You can also use `soaspec generate` to generate a set of tests from a WSDL. This is still in trial period and will be finished probably after Savon 3 is more stable.
|
61
|
+
|
62
|
+
## Usage
|
63
|
+
|
64
|
+
* SOAP - this uses Savon behind the scenes. Some defaults are overridden. Please see 'soap_handler.rb'-'default_options' method
|
65
|
+
for such defaults. When describing an API override this in 'savon_options' method
|
66
|
+
* REST - this uses the resource class from the Rest-Client gem behind the scenes.
|
67
|
+
|
68
|
+
See [spec](spec) and [features](features) for example of usage.
|
69
|
+
|
70
|
+
### ExchangeHandler
|
71
|
+
|
72
|
+
To start with, create a class inheriting from a ‘Handler’ class for each web service that needs testing.
|
73
|
+
In this class you define the common parameters used for testing it.
|
74
|
+
|
75
|
+
For example:
|
76
|
+
|
77
|
+
```ruby
|
78
|
+
# Classes are set up through inheriting from either `Soaspec::RestHandler` or `Soaspec::SoapHandler`
|
79
|
+
class PuppyService < Soaspec::RestHandler
|
80
|
+
# Set default headers for all `Exchanges` using this class
|
81
|
+
headers accept: 'application/json', content_type: 'application/json'
|
82
|
+
|
83
|
+
# URL for which all requests using this class will start with
|
84
|
+
base_url 'http://petstore.swagger.io/v2/pet'
|
85
|
+
|
86
|
+
# Accessing parts of a response
|
87
|
+
|
88
|
+
# Define a method 'id' that can be obtained with either XPATH '//id' or JSONPath '$..id'
|
89
|
+
element :id, :id
|
90
|
+
# Define method to obtain a category id through JSON Path
|
91
|
+
element :category_id, '$..category.id'
|
92
|
+
end
|
93
|
+
```
|
94
|
+
|
95
|
+
> You can easily create a exchange handler with the `soaspec add` command. This will also add comments explaining common methods that can be used
|
96
|
+
|
97
|
+
### Exchange
|
98
|
+
|
99
|
+
After creating the `ExchangeHandler`, you reference this class in creating `Exchange`s (objects that each represent a request / response pair).
|
100
|
+
Upon initialization of the Exchange object (or later on through setters), parameters specific to this request are set.
|
101
|
+
Most getters of the `Exchange` are on the response & will implicitly trigger the API request to be made.
|
102
|
+
Once this request has been made, all following accessors of the response will just use the response of the previous request made.
|
103
|
+
|
104
|
+
For example, to create a http post using the above `ExchangeHandler` and get parts of it's response.
|
105
|
+
```ruby
|
106
|
+
# Create a new Exchange that will post to 'http://petstore.swagger.io/v2/pet' with JSON { "status": "sold" }
|
107
|
+
# The 'body' key will convert it's value from a Hash to JSON
|
108
|
+
exchange = PuppyService.post(body: { status: 'sold' })
|
109
|
+
|
110
|
+
# This will trigger the request to be made & return the response, in this case a RestClient::Response object
|
111
|
+
response = exchange.response
|
112
|
+
# This will reuse the response already received return a value at JSON path $..category.id, throwing an exception if not found
|
113
|
+
exchange.category_id
|
114
|
+
# This will do the same but using path directly in code
|
115
|
+
exchange['$..category.id']
|
116
|
+
# Get the HTTP status code of the response
|
117
|
+
exchange.status_code
|
118
|
+
```
|
119
|
+
|
120
|
+
See [Request Body Parameters](https://gitlab.com/samuel-garratt/soaspec/wikis/RequestBodyParameters) for more details on setting a request body.
|
121
|
+
See [Creating an Exchange](https://gitlab.com/samuel-garratt/soaspec/wikis/CreatingExchange) for details on how to create an Exchange.
|
122
|
+
|
123
|
+
### Virtual Server
|
124
|
+
|
125
|
+
Soaspec includes a virtual server that returns REST and SOAP responses that can be used when learning or experimenting with API testing.
|
126
|
+
The gem itself uses this to test it's own functionality.
|
127
|
+
|
128
|
+
To start the server, after installing the gem, type
|
129
|
+
|
130
|
+
```
|
131
|
+
soaspec virtual_server [port_num]
|
132
|
+
```
|
133
|
+
|
134
|
+
By default it runs on port 4999. This will be used if `port_num` is empty.
|
135
|
+
|
136
|
+
You can look at the documentation for the web services provided at the rool url (e.g `localhost:4999`).
|
137
|
+
|
138
|
+
### RSpec
|
139
|
+
|
140
|
+
For example:
|
141
|
+
|
142
|
+
```ruby
|
143
|
+
context PuppyService.new('Order Puppies') do
|
144
|
+
describe post(:create_pet, body: { status: 'sold' }) do # Post with status as sold in request
|
145
|
+
its(['status']) { is_expected.to eq 'sold' } # Check responses status is sold
|
146
|
+
end
|
147
|
+
end
|
148
|
+
```
|
149
|
+
|
150
|
+
#### Tips
|
151
|
+
|
152
|
+
If you find having a large backtrace on errors or RSpec shared examples such as 'success scenarios' this can shorten the
|
153
|
+
backtrace.
|
154
|
+
|
155
|
+
RSpec.configure do |config|
|
156
|
+
|
157
|
+
config.backtrace_exclusion_patterns = [
|
158
|
+
/rspec/
|
159
|
+
]
|
160
|
+
end
|
161
|
+
|
162
|
+
### Cucumber
|
163
|
+
|
164
|
+
If you're using `Cucumber` then I would recommend the following
|
165
|
+
|
166
|
+
In the `Given` (or background) specify the `Exchange` object.
|
167
|
+
Either store this as an instance variable (e.g `@exchange`) or use the global `Soaspec.last_exchange` (which is automatically set).
|
168
|
+
|
169
|
+
In the `When`, use the `call` method to make the request `@exchange.call`. If problems occur in making the request this should separate such failures from issues with the response.
|
170
|
+
|
171
|
+
In the `Then`, make the assertions from the `@exchange` object.
|
172
|
+
E.g
|
173
|
+
```ruby
|
174
|
+
expect(@exchange['message']).to include 'success'
|
175
|
+
expect(@exchange.status_code).to eq 200
|
176
|
+
```
|
177
|
+
|
178
|
+
### Authentication
|
179
|
+
|
180
|
+
See [the wiki](https://gitlab.com/samuel-garratt/soaspec/wikis/Authentication) for details.
|
181
|
+
|
182
|
+
## Logging
|
183
|
+
|
184
|
+
By default traffic is logged to a file in the `logs` folder called `traffic_DATETIME.log`.
|
185
|
+
|
186
|
+
The `SpecLogger` class is responsible for handling all the logs.
|
187
|
+
|
188
|
+
To change the output file location to 'custom.log':
|
189
|
+
```ruby
|
190
|
+
Soaspec::SpecLogger.traffic_file = 'custom.log'
|
191
|
+
```
|
192
|
+
|
193
|
+
To change the folder the default log is:
|
194
|
+
|
195
|
+
```ruby
|
196
|
+
Soaspec::SpecLogger.traffic_folder = 'custom_folder'
|
197
|
+
```
|
198
|
+
|
199
|
+
To disable logging to a file set the 'output_to_file' to 'false'.
|
200
|
+
```ruby
|
201
|
+
Soaspec::SpecLogger.output_to_file = false
|
202
|
+
```
|
203
|
+
There is a similar parameter to logging to the terminal, STDOUT (by default false).
|
204
|
+
You can also change the color of this output with the `terminal_color` attribute.
|
205
|
+
```ruby
|
206
|
+
Soaspec::SpecLogger.output_to_terminal = true
|
207
|
+
Soaspec::SpecLogger.terminal_color = :magenta
|
208
|
+
```
|
209
|
+
|
210
|
+
## Learning more
|
211
|
+
|
212
|
+
Looking at the website [here](https://samuel-garratt.gitlab.io/soaspec/)
|
213
|
+
|
214
|
+
## Development
|
215
|
+
|
216
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
217
|
+
|
218
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
219
|
+
|
220
|
+
## Contributing
|
221
|
+
|
222
|
+
Bug reports and pull requests are welcome on GitLab at https://gitlab.com/samuel-garratt/soaspec. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
223
|
+
|
224
|
+
## License
|
225
|
+
|
226
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
227
|
+
|
228
|
+
## Code of Conduct
|
229
|
+
|
230
|
+
Everyone interacting in the Soaspec project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://gitlab.com/samuel-garratt/soaspec/blob/master/CODE_OF_CONDUCT.md).
|