optic-middleware 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +49 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/optic/middleware.rb +83 -0
- data/lib/optic/version.rb +5 -0
- data/optic-middleware.gemspec +26 -0
- metadata +97 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bd867496cc991cb06560b3ee9d479a288b50a4fa9b0ed17e1f123d8be46c9bc3
|
4
|
+
data.tar.gz: b9744fc4637bc22a2ff672e07836761c4a8da57329c041ecb674dc3dfc83718d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4349a7a8609289b828db44e2925c88ec799c46c68b6828564b9230ed2b383f23c03d350e0f1b88a9a81dcce89e6db45a58a200842548b078dc778ddcd6eddca5
|
7
|
+
data.tar.gz: 14fd40a4919697dbd1571a9f88590570cef9421914d6e3d696e4bacdf367634702014e2fbee520cb2d535f07d12a638343346367f90e26e5b11341eaa460a762
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Aidan Cunniffe
|
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
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Optic::Middleware
|
2
|
+
|
3
|
+
Rails projects or those built on top of Rack are easy to connect to Optic using our custom middleware. In this tutorial we'll show you how to connect the Optic Documenting Middleware to your Rack API so that your integration tests document your code as they execute.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'optic-middleware'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install optic-middleware
|
20
|
+
|
21
|
+
|
22
|
+
### Making the Middleware Run During Testing
|
23
|
+
Now add the middleware to your Rack configuration, in a Rails app it's best to do this in `config/enviroments/test.rb` so the middleware is only used during testing.
|
24
|
+
|
25
|
+
> Note: If you use multiple middlewares, make sure the Documenting Middleware is added at the bottom of the stack so the documentation is accurate. In most cases putting it last in your configuration file is sufficient.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
Rails.application.configure do {
|
29
|
+
# All your current configuration settings...
|
30
|
+
|
31
|
+
# The Documenting middleware. Only used if 'OPTIC_SERVER_LISTENING' flag is found in ENV.
|
32
|
+
if ENV['OPTIC_SERVER_LISTENING']
|
33
|
+
config.middleware.use OpticTestFixture::DocumentingMiddleware
|
34
|
+
end
|
35
|
+
}
|
36
|
+
```
|
37
|
+
|
38
|
+
> Note for RSpec users: Optic will only document specs of type :request since only integration tests contain enough data to generate REST docs. Specs of type :controller skip the Rack stack so documentation generated from them would be incomplete.
|
39
|
+
|
40
|
+
## Using the Proxy Fixture
|
41
|
+
The Documenting middleware will document all the requests/responses that your tests run. Since it is integrated at the middleware level there's no need to update any of your tests files or fixtures.
|
42
|
+
|
43
|
+
## Contributing
|
44
|
+
|
45
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/opticdev/optic-middleware.
|
46
|
+
|
47
|
+
## License
|
48
|
+
|
49
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "optic/middleware"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,83 @@
|
|
1
|
+
require "optic/version"
|
2
|
+
require 'net/http'
|
3
|
+
|
4
|
+
module Optic
|
5
|
+
class DocumentingMiddleware
|
6
|
+
def initialize(app, options = {})
|
7
|
+
@app = app
|
8
|
+
end
|
9
|
+
|
10
|
+
def call(env)
|
11
|
+
req = Rack::Request.new(env)
|
12
|
+
res = @app.call(env)
|
13
|
+
# Log request and response
|
14
|
+
logRequestAndResponse(req, res)
|
15
|
+
# Return Response without changing
|
16
|
+
res
|
17
|
+
end
|
18
|
+
|
19
|
+
private
|
20
|
+
|
21
|
+
def headerHash(message)
|
22
|
+
Hash[*message.select {|k, v| k.start_with? 'HTTP_'}
|
23
|
+
.collect {|k, v| [k.sub(/^HTTP_/, ''), v]}
|
24
|
+
.collect {|k, v| [k.split('_').collect(&:capitalize).join('-'), v]}
|
25
|
+
.sort
|
26
|
+
.flatten]
|
27
|
+
end
|
28
|
+
|
29
|
+
def addHeaders(headers, request)
|
30
|
+
headers.each do |key, value|
|
31
|
+
request.add_field(key, value)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def logRequestAndResponse(req, res)
|
36
|
+
|
37
|
+
# Log Request to Optic
|
38
|
+
|
39
|
+
logging_request = Net::HTTP.const_get(req.request_method.capitalize).new(req.fullpath)
|
40
|
+
# Include Headers
|
41
|
+
addHeaders(headerHash(req.env), logging_request)
|
42
|
+
# Include body
|
43
|
+
if logging_request.request_body_permitted? && req.body
|
44
|
+
logging_request.body_stream = req.body
|
45
|
+
logging_request.content_length = req.content_length.to_i
|
46
|
+
logging_request.content_type = req.content_type if req.content_type
|
47
|
+
logging_request.body_stream.rewind
|
48
|
+
end
|
49
|
+
|
50
|
+
# Send request to request logging endpoint
|
51
|
+
http = Net::HTTP.new("localhost", 30334)
|
52
|
+
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
|
53
|
+
|
54
|
+
logging_request_response = http.start do
|
55
|
+
http.request(logging_request)
|
56
|
+
end
|
57
|
+
|
58
|
+
logging_request_response.code
|
59
|
+
# Save the ID Optic assigns this request
|
60
|
+
interactionId = logging_request_response.body || ""
|
61
|
+
|
62
|
+
# Log Response to Optic
|
63
|
+
resStatus, resHeaders, resBody = res
|
64
|
+
|
65
|
+
logging_response = Net::HTTP.const_get("Post").new("/interactions/" + interactionId + "/status/" + resStatus.to_s)
|
66
|
+
addHeaders(resHeaders, logging_response)
|
67
|
+
if logging_response.request_body_permitted? && resBody
|
68
|
+
bodyData = ""
|
69
|
+
resBody.each do |line|
|
70
|
+
bodyData << line
|
71
|
+
end
|
72
|
+
logging_response.body = bodyData
|
73
|
+
logging_response.content_length = bodyData.bytesize.to_s
|
74
|
+
end
|
75
|
+
|
76
|
+
http = Net::HTTP.new("localhost", 30335)
|
77
|
+
logging_response_response = http.start do
|
78
|
+
http.request(logging_response)
|
79
|
+
end
|
80
|
+
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "optic/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "optic-middleware"
|
8
|
+
spec.version = Optic::Middleware::VERSION
|
9
|
+
spec.authors = ["Aidan Cunniffe"]
|
10
|
+
spec.email = ["acunniffe@useoptic.com"]
|
11
|
+
|
12
|
+
spec.summary = "Document an API written in Rack/Rails by running tests"
|
13
|
+
spec.description = ""
|
14
|
+
spec.homepage = "https://github.com/opticdev/ruby-rack-fixture"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
spec.add_development_dependency "bundler", "~> 2.0"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: optic-middleware
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Aidan Cunniffe
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-03-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: ''
|
56
|
+
email:
|
57
|
+
- acunniffe@useoptic.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README.md
|
68
|
+
- Rakefile
|
69
|
+
- bin/console
|
70
|
+
- bin/setup
|
71
|
+
- lib/optic/middleware.rb
|
72
|
+
- lib/optic/version.rb
|
73
|
+
- optic-middleware.gemspec
|
74
|
+
homepage: https://github.com/opticdev/ruby-rack-fixture
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubygems_version: 3.0.3
|
94
|
+
signing_key:
|
95
|
+
specification_version: 4
|
96
|
+
summary: Document an API written in Rack/Rails by running tests
|
97
|
+
test_files: []
|