rreplay 0.1.0
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 +7 -0
- data/.gitignore +9 -0
- data/.ruby-version +1 -0
- data/.travis.yml +6 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +38 -0
- data/LICENSE.txt +21 -0
- data/README.md +70 -0
- data/Rakefile +11 -0
- data/bin/console +14 -0
- data/bin/rreplay +64 -0
- data/bin/setup +8 -0
- data/lib/rack/rreplay.rb +125 -0
- data/lib/rreplay/debugger.rb +14 -0
- data/lib/rreplay/format.rb +28 -0
- data/lib/rreplay/replay_runner.rb +107 -0
- data/lib/rreplay.rb +3 -0
- data/rreplay.gemspec +34 -0
- metadata +180 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: df1ba81f052a9eca78ff75466a5f3cb5d79d8eac293fc88e991153649f3074f5
|
4
|
+
data.tar.gz: 1c2dcdc2a6d633750c7fdfc116c90de736a95bd2f73c01b8202445675f5970d0
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 66e8b80d877d22e33335e360301143dabd273a8c9caf451b0b9437e831d5a9e7669332ccec8c18e977b6b6a54ea101aed68ed937784e3972f5fe10ca57b5b944
|
7
|
+
data.tar.gz: 4691481c1a6cd91c6e7e0022b896f027446b79f28262b7b9644ed89b66a6b14aa1d2368fbad0600127f6e606cfdceb5655cfe9721cab235c5fe69b04d968c999
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.7.0
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at violethero0820@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [https://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: https://contributor-covenant.org
|
74
|
+
[version]: https://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
rreplay (0.1.0)
|
5
|
+
msgpack
|
6
|
+
rack
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
byebug (11.0.1)
|
12
|
+
json_expressions (0.9.0)
|
13
|
+
minitest (5.13.0)
|
14
|
+
msgpack (1.3.1)
|
15
|
+
rack (2.0.8)
|
16
|
+
rack-test (1.1.0)
|
17
|
+
rack (>= 1.0, < 3)
|
18
|
+
rake (12.3.3)
|
19
|
+
timecop (0.9.1)
|
20
|
+
|
21
|
+
PLATFORMS
|
22
|
+
ruby
|
23
|
+
|
24
|
+
DEPENDENCIES
|
25
|
+
bundler
|
26
|
+
byebug
|
27
|
+
json_expressions
|
28
|
+
minitest (~> 5.0)
|
29
|
+
rack-test
|
30
|
+
rake (~> 12.0)
|
31
|
+
rreplay!
|
32
|
+
timecop
|
33
|
+
|
34
|
+
RUBY VERSION
|
35
|
+
ruby 2.7.0p0
|
36
|
+
|
37
|
+
BUNDLED WITH
|
38
|
+
2.1.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 petitviolet
|
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,70 @@
|
|
1
|
+
# Rreplay
|
2
|
+
|
3
|
+
Replay HTTP request and is able to assert if the response is expected.
|
4
|
+
|
5
|
+
- Record HTTP request and response into file
|
6
|
+
- Feed the recorded request and response
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'rreplay'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
$ bundle install
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
|
22
|
+
$ gem install rreplay
|
23
|
+
|
24
|
+
## Usage
|
25
|
+
|
26
|
+
### HTTP request/resopnse recording
|
27
|
+
|
28
|
+
Use `Rack::Rreplay`.
|
29
|
+
|
30
|
+
In 'config.ru' file:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
require 'rack/rreplay'
|
34
|
+
use Rack::Rreplay.Middleware(directory: './rreplay_tmp', format: :json),
|
35
|
+
{ sample: 5, extra_header_keys: %w[ACCESS_TOKEN], debug: true }
|
36
|
+
```
|
37
|
+
|
38
|
+
This configuration enables rreplay to record HTTP request and response, and write records into `./rreplay_tmp` directory files with specified format(json or msgpack).
|
39
|
+
|
40
|
+
Please see the implementation of [rack/rreplay.rb](https://github.com/petitviolet/rreplay/blob/master/lib/rack/rreplay.rb).
|
41
|
+
|
42
|
+
### HTTP request replay
|
43
|
+
|
44
|
+
Use `bundle exec rreplay <endpoint> <target>` to send recorded HTTP request in <target> to <endpoint>.
|
45
|
+
|
46
|
+
```console
|
47
|
+
$ bundle exec rreplay 'http://localhost:4567' ./rreplay_tmp/ --format json --verbose
|
48
|
+
$ bundle exec rreplay 'https://example.com' ./rreplay_tmp/rreplay.log.msgpack --format msgpack
|
49
|
+
```
|
50
|
+
|
51
|
+
Please call `bundle exec rreplay --help` to see arguments and available options.
|
52
|
+
|
53
|
+
## Development
|
54
|
+
|
55
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
56
|
+
|
57
|
+
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).
|
58
|
+
|
59
|
+
## Contributing
|
60
|
+
|
61
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/petitviolet/rreplay. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/petitviolet/rreplay/blob/master/CODE_OF_CONDUCT.md).
|
62
|
+
|
63
|
+
|
64
|
+
## License
|
65
|
+
|
66
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
67
|
+
|
68
|
+
## Code of Conduct
|
69
|
+
|
70
|
+
Everyone interacting in the Rreplay project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/petitviolet/rreplay/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rreplay"
|
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/rreplay
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'optparse'
|
4
|
+
require "bundler/setup"
|
5
|
+
require "rreplay/replay_runner"
|
6
|
+
|
7
|
+
def main(endpoint, target, format: :msgpack, verbose: false)
|
8
|
+
runner = Rreplay::ReplayRunner.new(endpoint, target, format: format, debug: verbose)
|
9
|
+
runner.run
|
10
|
+
end
|
11
|
+
|
12
|
+
class CLI
|
13
|
+
def initialize(argv = ARGV)
|
14
|
+
@argv = argv
|
15
|
+
@opts = {
|
16
|
+
format: :msgpack,
|
17
|
+
verbose: false,
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def inputs
|
22
|
+
args = parser.parse(@argv)
|
23
|
+
|
24
|
+
endpoint = args.shift
|
25
|
+
target = args.shift
|
26
|
+
|
27
|
+
show_usage if (endpoint.nil? || target.nil?)
|
28
|
+
|
29
|
+
[{ endpoint: endpoint, target: target }, @opts]
|
30
|
+
rescue => e
|
31
|
+
show_usage(e)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def parser
|
37
|
+
return @parser if @parser
|
38
|
+
|
39
|
+
opt = OptionParser.new
|
40
|
+
opt.banner = "Usage: #{__FILE__} <endpoint> <target> [options]"
|
41
|
+
opt.on_head(
|
42
|
+
"arguments:",
|
43
|
+
"#{opt.summary_indent}endpoint: root URI to send HTTP requests. e.g. https://example.com, http://www.example.org",
|
44
|
+
"#{opt.summary_indent}target: HTTP requests dump file/directory using Rack::Rreplay middleware.",
|
45
|
+
)
|
46
|
+
opt.separator('options:')
|
47
|
+
opt.on('-h', '--help', 'show this help') { |_| show_usage }
|
48
|
+
opt.on('-f', '--format VALUE', [:json, :msgpack], 'json or msgpack') { |v| @opts[:format] = v.to_sym }
|
49
|
+
opt.on('-v', '--[no-]verbose', 'show debug logs') { |v| @opts[:verbose] = v }
|
50
|
+
@parser = opt
|
51
|
+
end
|
52
|
+
|
53
|
+
def show_usage(error = nil)
|
54
|
+
puts "error: #{error.message}\n" if error
|
55
|
+
puts <<~EOF
|
56
|
+
#{parser.to_s}
|
57
|
+
EOF
|
58
|
+
exit 1
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
args, opts = CLI.new.inputs
|
63
|
+
|
64
|
+
main(args[:endpoint], args[:target], format: opts[:format], verbose: opts[:verbose])
|
data/bin/setup
ADDED
data/lib/rack/rreplay.rb
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'securerandom'
|
4
|
+
require 'msgpack'
|
5
|
+
require 'json'
|
6
|
+
require 'time'
|
7
|
+
require_relative '../rreplay'
|
8
|
+
require_relative '../rreplay/debugger'
|
9
|
+
require_relative '../rreplay/format'
|
10
|
+
|
11
|
+
module Rack
|
12
|
+
class Rreplay
|
13
|
+
|
14
|
+
class << self
|
15
|
+
# ==sample
|
16
|
+
# use Rack::Rreplay.Middleware(directory: './tmp', format: :json),
|
17
|
+
# sample: 5, extra_header_keys: %w[ACCESS_TOKEN], debug: true
|
18
|
+
#
|
19
|
+
# @param directory [String] rreplay dump file directory, and if nil, use logger as debug
|
20
|
+
# @param logger [IO] if directory is nil, logger can be given
|
21
|
+
def Middleware(directory:, format: :msgpack, logger: nil)
|
22
|
+
format = ::Rreplay::Format.of(format)
|
23
|
+
if directory
|
24
|
+
::FileUtils.mkdir_p(directory)
|
25
|
+
logger = ::Logger::LogDevice.new(
|
26
|
+
::File.join(directory, ::Rreplay::LOG_FILE_NAME_PREFIX + format.file_suffix),
|
27
|
+
shift_age: 10,
|
28
|
+
shift_size: 1048576,
|
29
|
+
)
|
30
|
+
else
|
31
|
+
logger = logger
|
32
|
+
end
|
33
|
+
class_definition(logger, format)
|
34
|
+
end
|
35
|
+
|
36
|
+
private def class_definition(logger, format)
|
37
|
+
Class.new do
|
38
|
+
@@counter = 0
|
39
|
+
@@logger = logger
|
40
|
+
@@format = format
|
41
|
+
|
42
|
+
# @params kwargs[:sample] [Integer] output sample (if 10, output a log once every 10 requests)
|
43
|
+
# @params kwargs[:extra_header_keys] [Array[String]] more header keys
|
44
|
+
# @params kwargs[:format] :msgpack | :json
|
45
|
+
# @params kwargs[:debug] if true, output debugging logs to stderr
|
46
|
+
def initialize(app, **kwargs)
|
47
|
+
@app = app
|
48
|
+
@debugger = ::Rreplay::Debugger.new($stderr, kwargs[:debug] || false)
|
49
|
+
@sample = kwargs[:sample] || 10
|
50
|
+
@extra_header_keys = kwargs[:extra_header_keys] || []
|
51
|
+
end
|
52
|
+
|
53
|
+
def call(env)
|
54
|
+
start_time = Time.now
|
55
|
+
@app.call(env).tap do |res|
|
56
|
+
write(start_time, env, res)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
private
|
61
|
+
|
62
|
+
def write(start_time, env, res)
|
63
|
+
payload = nil
|
64
|
+
if (@@counter % @sample).zero?
|
65
|
+
payload = serialize(start_time, env, res)
|
66
|
+
@@logger.write(payload + "\n")
|
67
|
+
end
|
68
|
+
@@counter += 1
|
69
|
+
|
70
|
+
@debugger.out do
|
71
|
+
payload ||= serialize(start_time, env, res)
|
72
|
+
"[Rreplay DEBUG]#{Time.now}: counter: #{@@counter}, sample: #{@sample}, payload: #{payload}"
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def serialize(start_time, env, res)
|
77
|
+
uuid = SecureRandom.uuid
|
78
|
+
end_time = Time.now
|
79
|
+
|
80
|
+
hash = {
|
81
|
+
'uuid' => uuid,
|
82
|
+
'time' => end_time.iso8601,
|
83
|
+
'response_time' => (end_time - start_time).to_s,
|
84
|
+
'request' => request_hash(env),
|
85
|
+
'response' => response_hash(res)
|
86
|
+
}
|
87
|
+
@@format.serializer.call(hash)
|
88
|
+
end
|
89
|
+
|
90
|
+
def response_hash(res)
|
91
|
+
status, headers, body = res
|
92
|
+
{
|
93
|
+
'status' => status,
|
94
|
+
'headers' => headers,
|
95
|
+
'body' => body.join(''),
|
96
|
+
}
|
97
|
+
end
|
98
|
+
|
99
|
+
def request_hash(env)
|
100
|
+
headers = {
|
101
|
+
'content-type' => env['CONTENT_TYPE'],
|
102
|
+
'cookie' => env['HTTP_COOKIE'],
|
103
|
+
'user-agent' => env['HTTP_USER_AGENT'],
|
104
|
+
}
|
105
|
+
@extra_header_keys.each do |key|
|
106
|
+
headers.merge!(key => env["HTTP_#{key}"])
|
107
|
+
end
|
108
|
+
|
109
|
+
{
|
110
|
+
'method' => env['REQUEST_METHOD'],
|
111
|
+
'path' => env['PATH_INFO'],
|
112
|
+
'body' => env['rack.input'].gets,
|
113
|
+
'query_strings' => env['QUERY_STRING'].empty? ? '' : '?' + env['QUERY_STRING'],
|
114
|
+
'headers' => headers
|
115
|
+
}
|
116
|
+
end
|
117
|
+
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
private
|
123
|
+
|
124
|
+
end
|
125
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Rreplay
|
2
|
+
class Debugger
|
3
|
+
def initialize(logger, debug = true)
|
4
|
+
@logger = logger
|
5
|
+
@debug = !logger.nil? && debug
|
6
|
+
end
|
7
|
+
|
8
|
+
def out(&block)
|
9
|
+
return unless @debug
|
10
|
+
|
11
|
+
@logger.write("#{Time.now.iso8601} - #{block.call}\n")
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Rreplay
|
2
|
+
module Format
|
3
|
+
Format = Struct.new(:file_suffix, :serializer, :deserializer)
|
4
|
+
|
5
|
+
Json = Format.new('.json',
|
6
|
+
->(str) { JSON.dump(str) },
|
7
|
+
->(str) { JSON.parse(str) },
|
8
|
+
)
|
9
|
+
Msgpack = Format.new('.msgpack',
|
10
|
+
->(str) { MessagePack.pack(str) },
|
11
|
+
->(str) { MessagePack.unpack(str) },
|
12
|
+
)
|
13
|
+
|
14
|
+
class << self
|
15
|
+
def of(format)
|
16
|
+
case format.to_sym
|
17
|
+
when :json
|
18
|
+
Json
|
19
|
+
when :msgpack
|
20
|
+
Msgpack
|
21
|
+
else
|
22
|
+
raise "Unknown format #{format}"
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
require 'rack/rreplay'
|
2
|
+
require 'msgpack'
|
3
|
+
require 'json'
|
4
|
+
require 'net/http'
|
5
|
+
require 'uri'
|
6
|
+
require_relative './debugger'
|
7
|
+
require_relative './format'
|
8
|
+
|
9
|
+
module Rreplay
|
10
|
+
class ReplayRunner
|
11
|
+
def initialize(endpoint, target, format: :msgpack, debug: false)
|
12
|
+
@endpoint = endpoint
|
13
|
+
@format = Rreplay::Format.of(format)
|
14
|
+
@target = target
|
15
|
+
@debugger = Debugger.new($stderr, debug)
|
16
|
+
end
|
17
|
+
|
18
|
+
def run
|
19
|
+
file_names.each do |file_name|
|
20
|
+
::File.open(file_name) do |file|
|
21
|
+
@debugger.out { "Open file: #{file_name}" }
|
22
|
+
|
23
|
+
file.each_line do |line|
|
24
|
+
next if line.start_with?('#') # LogDevice's header
|
25
|
+
line.chomp!
|
26
|
+
|
27
|
+
begin
|
28
|
+
record = @format.deserializer.call(line)
|
29
|
+
rescue => e
|
30
|
+
raise "Failed to deserialize. err = #{e.inspect}, line = #{line}", e
|
31
|
+
end
|
32
|
+
request = record["request"]
|
33
|
+
result, response_time = http_call(request)
|
34
|
+
@debugger.out {
|
35
|
+
response_json = {
|
36
|
+
status: result.code,
|
37
|
+
headers: record['response']['headers'].reduce({}) do |acc, (key, _)|
|
38
|
+
acc.merge({key => result[key]})
|
39
|
+
end,
|
40
|
+
body: Array(result.body),
|
41
|
+
}
|
42
|
+
<<~EOF
|
43
|
+
#{record['uuid']}:
|
44
|
+
* request:
|
45
|
+
#{request}
|
46
|
+
* response(actual):
|
47
|
+
#{response_time} sec
|
48
|
+
#{response_json}
|
49
|
+
* response(recorded):
|
50
|
+
#{record['response_time']} sec
|
51
|
+
#{record['response']}
|
52
|
+
EOF
|
53
|
+
}
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def file_names
|
62
|
+
if ::File.directory?(@target)
|
63
|
+
::Dir.glob(::File.join(@target,
|
64
|
+
::Rreplay::LOG_FILE_NAME_PREFIX + @format.file_suffix + "*"))
|
65
|
+
else
|
66
|
+
Array(@target)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
def http_call(orig_request)
|
72
|
+
uri = URI(::File.join(@endpoint, orig_request['path'], orig_request['query_strings']))
|
73
|
+
body = orig_request['body']
|
74
|
+
headers = orig_request['headers']
|
75
|
+
headers.merge!({ 'User-Agent': 'RreplayRunner' })
|
76
|
+
|
77
|
+
request_clazz = case orig_request['method'].upcase
|
78
|
+
when 'GET'
|
79
|
+
Net::HTTP::Get
|
80
|
+
when 'POST'
|
81
|
+
Net::HTTP::Post
|
82
|
+
when 'PUT'
|
83
|
+
Net::HTTP::Put
|
84
|
+
when 'PATCH'
|
85
|
+
Net::HTTP::Patch
|
86
|
+
when 'DELETE'
|
87
|
+
Net::HTTP::Delete
|
88
|
+
else
|
89
|
+
# ignore
|
90
|
+
return
|
91
|
+
end
|
92
|
+
request = request_clazz.new(uri, headers).tap do |req|
|
93
|
+
req.body = body
|
94
|
+
end
|
95
|
+
|
96
|
+
start_time = Time.now
|
97
|
+
response = Net::HTTP.start(uri.hostname, uri.port,
|
98
|
+
:use_ssl => uri.scheme == 'https') { |http|
|
99
|
+
http.request(request)
|
100
|
+
}
|
101
|
+
[response, Time.now - start_time]
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
105
|
+
private
|
106
|
+
|
107
|
+
end
|
data/lib/rreplay.rb
ADDED
data/rreplay.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
Gem::Specification.new do |spec|
|
2
|
+
spec.name = "rreplay"
|
3
|
+
spec.version = "0.1.0"
|
4
|
+
spec.authors = ["petitviolet"]
|
5
|
+
spec.email = ["violethero0820@gmail.com"]
|
6
|
+
|
7
|
+
spec.summary = %q{A rack middleware and replayer HTTP request/response}
|
8
|
+
spec.description = %q{A rack middleware to dump request and response, and replayer of recorded requests}
|
9
|
+
spec.homepage = "https://github.com/petitviolet/rreplay"
|
10
|
+
spec.license = "MIT"
|
11
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
12
|
+
|
13
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
14
|
+
spec.metadata["source_code_uri"] = "https://github.com/petitviolet/rreplay"
|
15
|
+
spec.metadata["changelog_uri"] = "https://github.com/petitviolet/rreplay"
|
16
|
+
|
17
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
18
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(spec|test|example)/}) }
|
19
|
+
end
|
20
|
+
spec.bindir = "bin"
|
21
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
24
|
+
|
25
|
+
spec.add_dependency "rack"
|
26
|
+
spec.add_dependency "msgpack"
|
27
|
+
|
28
|
+
spec.add_development_dependency "bundler"
|
29
|
+
spec.add_development_dependency "rake"
|
30
|
+
spec.add_development_dependency "rack-test"
|
31
|
+
spec.add_development_dependency "json_expressions"
|
32
|
+
spec.add_development_dependency "byebug"
|
33
|
+
spec.add_development_dependency "timecop"
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,180 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rreplay
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- petitviolet
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-12-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rack
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: msgpack
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rack-test
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: json_expressions
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: byebug
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: timecop
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
description: A rack middleware to dump request and response, and replayer of recorded
|
126
|
+
requests
|
127
|
+
email:
|
128
|
+
- violethero0820@gmail.com
|
129
|
+
executables:
|
130
|
+
- console
|
131
|
+
- rreplay
|
132
|
+
- setup
|
133
|
+
extensions: []
|
134
|
+
extra_rdoc_files: []
|
135
|
+
files:
|
136
|
+
- ".gitignore"
|
137
|
+
- ".ruby-version"
|
138
|
+
- ".travis.yml"
|
139
|
+
- CODE_OF_CONDUCT.md
|
140
|
+
- Gemfile
|
141
|
+
- Gemfile.lock
|
142
|
+
- LICENSE.txt
|
143
|
+
- README.md
|
144
|
+
- Rakefile
|
145
|
+
- bin/console
|
146
|
+
- bin/rreplay
|
147
|
+
- bin/setup
|
148
|
+
- lib/rack/rreplay.rb
|
149
|
+
- lib/rreplay.rb
|
150
|
+
- lib/rreplay/debugger.rb
|
151
|
+
- lib/rreplay/format.rb
|
152
|
+
- lib/rreplay/replay_runner.rb
|
153
|
+
- rreplay.gemspec
|
154
|
+
homepage: https://github.com/petitviolet/rreplay
|
155
|
+
licenses:
|
156
|
+
- MIT
|
157
|
+
metadata:
|
158
|
+
homepage_uri: https://github.com/petitviolet/rreplay
|
159
|
+
source_code_uri: https://github.com/petitviolet/rreplay
|
160
|
+
changelog_uri: https://github.com/petitviolet/rreplay
|
161
|
+
post_install_message:
|
162
|
+
rdoc_options: []
|
163
|
+
require_paths:
|
164
|
+
- lib
|
165
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
166
|
+
requirements:
|
167
|
+
- - ">="
|
168
|
+
- !ruby/object:Gem::Version
|
169
|
+
version: 2.3.0
|
170
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
171
|
+
requirements:
|
172
|
+
- - ">="
|
173
|
+
- !ruby/object:Gem::Version
|
174
|
+
version: '0'
|
175
|
+
requirements: []
|
176
|
+
rubygems_version: 3.1.2
|
177
|
+
signing_key:
|
178
|
+
specification_version: 4
|
179
|
+
summary: A rack middleware and replayer HTTP request/response
|
180
|
+
test_files: []
|