rack-translating_proxy 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 794652ff10fbf63964576fba30599ee1d5c34f67
4
+ data.tar.gz: fae17b5582bba5f993ad85bcd29d5212b45d5fde
5
+ SHA512:
6
+ metadata.gz: 2d9db4752f01be1054c5f95d0e42e1f4b99fa999dc271e7887121f9de0f58562a6a08f3c91d69231475d6386ac20f016ad248a0a6e042b4999dfbdd000eaa78c
7
+ data.tar.gz: 3629f9946d3e9739322fa13a04d9b61f5fa46fa1dc52a12fb651d6f9ddb079b1e5b6f7bda318d9741fff016af0644028494ef341a7c67fa2672c64c86b736e8d
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --color
2
+ --warnings
3
+ --require spec_helper
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rspec'
6
+ gem 'rspec-its'
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Mike Nicholaides
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,30 @@
1
+ # Rack::TranslatingProxy
2
+
3
+ A Proxy that `gsub`s the contents of the requests and responses in order to make a mostly transparent proxy.
4
+ Its purpose is to aid in development and testing of integration with external services, especially when the browser has to interact with those services directly.
5
+
6
+ ## Usage
7
+
8
+ TODO: Write usage instructions here
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'rack-translating_proxy'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install rack-translating_proxy
23
+
24
+ ## Contributing
25
+
26
+ 1. Fork it ( http://github.com/<my-github-username>/rack-translating_proxy/fork )
27
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
28
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
29
+ 4. Push to the branch (`git push origin my-new-feature`)
30
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,100 @@
1
+ require 'rack/proxy'
2
+
3
+ module Rack
4
+ class TranslatingProxy < Rack::Proxy
5
+ def rewrite_env(env)
6
+ rewrite_request_url env
7
+ rewrite_request_body env
8
+ rewrite_request_query_string env
9
+ rewrite_request_path env
10
+
11
+ env
12
+ end
13
+
14
+ def rewrite_request_url(env)
15
+ env['HTTP_HOST'] = _target_host.host
16
+ env['SERVER_PORT'] = _target_host.port
17
+ env['rack.url_scheme'] = _target_host.scheme
18
+ end
19
+
20
+ def rewrite_request_body(env)
21
+ rewritten_input = rewrite_request_string(io(env['rack.input']).read)
22
+
23
+ env['rack.input'] = io(rewritten_input)
24
+ env['CONTENT_LENGTH'] = rewritten_input.size
25
+ end
26
+
27
+ def rewrite_request_string(str)
28
+ rewrite_string(str, request_mapping, URI.method(:encode_www_form_component))
29
+ end
30
+
31
+ def rewrite_request_query_string(env)
32
+ env['QUERY_STRING'] = rewrite_request_string(env['QUERY_STRING'])
33
+ end
34
+
35
+ def rewrite_request_path(env)
36
+ env['REQUEST_URI'] = rewrite_request_string(env['REQUEST_URI'])
37
+ end
38
+
39
+ def rewrite_string(string, mapping, transform = proc { |x| x })
40
+ mapping = mapping.map do |source, target|
41
+ [transform[source], transform[target]]
42
+ end
43
+
44
+ mapping.each do |source, target|
45
+ string = string.gsub(source, target)
46
+ end
47
+
48
+ string
49
+ end
50
+
51
+ def io(stringy_thing)
52
+ if stringy_thing.respond_to?(:read)
53
+ stringy_thing
54
+ else
55
+ StringIO.new(stringy_thing)
56
+ end
57
+ end
58
+
59
+ def rewrite_response(triplet)
60
+ status, headers, body = triplet
61
+ rewrite_response_location headers
62
+ remove_interfering_response_headers headers
63
+ [status, headers, body]
64
+ end
65
+
66
+ def rewrite_response_location(headers)
67
+ if headers['location']
68
+ headers['location'] = headers['location'].map do |location|
69
+ rewrite_string(location, _response_mapping)
70
+ end
71
+ end
72
+ end
73
+
74
+ def remove_interfering_response_headers(headers)
75
+ headers.reject! do |key, _|
76
+ %w[status connection transfer-encoding].include?(key)
77
+ end
78
+ end
79
+
80
+ def request_mapping
81
+ fail 'Not implemented'
82
+ end
83
+
84
+ def _request_mappping
85
+ @_request_mappping ||= request_mappping
86
+ end
87
+
88
+ def _response_mapping
89
+ @_response_mapping ||= request_mapping.invert
90
+ end
91
+
92
+ def _target_host
93
+ @_target_host ||= URI(target_host)
94
+ end
95
+
96
+ def target_host
97
+ fail 'Not implemented'
98
+ end
99
+ end
100
+ end
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rack-translating_proxy"
7
+ spec.version = '0.1.0'
8
+ spec.authors = ["Mike Nicholaides"]
9
+ spec.email = ["mike.nicholaides@gmail.com"]
10
+ spec.summary = 'Proxy that translates strings'
11
+ spec.description = 'Proxy that translates strings'
12
+ spec.homepage = ""
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency 'rack-proxy', '~> 0.5'
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.5"
23
+ spec.add_development_dependency "rake"
24
+ end
@@ -0,0 +1,25 @@
1
+ RSpec.configure do |config|
2
+ config.filter_run :focus
3
+ config.run_all_when_everything_filtered = true
4
+
5
+ if config.files_to_run.one?
6
+ config.default_formatter = 'doc'
7
+ end
8
+
9
+ config.order = :random
10
+
11
+ Kernel.srand config.seed
12
+
13
+ config.expect_with :rspec do |expectations|
14
+ expectations.syntax = :expect
15
+ end
16
+
17
+ config.mock_with :rspec do |mocks|
18
+ mocks.syntax = :expect
19
+ mocks.verify_partial_doubles = true
20
+ end
21
+ end
22
+
23
+ require 'rspec/its'
24
+
25
+ require 'rack/translating_proxy'
@@ -0,0 +1,90 @@
1
+ class ExampleTranslatingProxy < Rack::TranslatingProxy
2
+ def target_host
3
+ 'https://target-host.url'
4
+ end
5
+
6
+ def request_mapping
7
+ {
8
+ 'http://proxy.url:1234' => 'https://target.url:4321',
9
+ 'http://proxy.url:5678' => 'https://target.url:8765',
10
+ 'a word' => 'another word'
11
+ }
12
+ end
13
+ end
14
+
15
+ describe ExampleTranslatingProxy do
16
+ let(:app) { described_class.new }
17
+
18
+ describe "#rewrite_env" do
19
+ subject { app.rewrite_env(Hash.new { '' }.merge(env)) }
20
+ let(:env) { {} }
21
+
22
+ describe "rewriting the uri" do
23
+ its(['HTTP_HOST']) { is_expected.to eq 'target-host.url' }
24
+ its(['SERVER_PORT']) { is_expected.to eq 443 }
25
+ its(['rack.url_scheme']) { is_expected.to eq 'https' }
26
+ end
27
+
28
+ describe "rewriting the path" do
29
+ let(:env) { { 'REQUEST_URI' => '/path/with/a+word' } }
30
+ its(['REQUEST_URI']) { is_expected.to eq '/path/with/another+word' }
31
+ end
32
+
33
+ describe "rewriting the query string" do
34
+ let(:env) { { 'QUERY_STRING' => 'a+word&something-else' } }
35
+ its(['QUERY_STRING']) { is_expected.to eq 'another+word&something-else' }
36
+ end
37
+
38
+ describe "rewriting the body" do
39
+ context 'when it is a string' do
40
+ let(:env) { { 'rack.input' => 'my+string+with+a+word' } }
41
+ specify do
42
+ expect(subject['rack.input'].read).to eq 'my+string+with+another+word'
43
+ end
44
+
45
+ its(['CONTENT_LENGTH']) { is_expected.to eq 27 }
46
+ end
47
+
48
+ context 'when it is a StringIO' do
49
+ let(:env) { { 'rack.input' => StringIO.new('my+string+with+a+word') } }
50
+ specify do
51
+ expect(subject['rack.input'].read).to eq 'my+string+with+another+word'
52
+ end
53
+
54
+ its(['CONTENT_LENGTH']) { is_expected.to eq 27 }
55
+ end
56
+ end
57
+ end
58
+
59
+ describe "#rewrite_response" do
60
+ subject { app.rewrite_response(triplet) }
61
+
62
+ let(:triplet) { [given_status, given_headers, given_body] }
63
+ let(:given_status) { double }
64
+ let(:given_body) { double }
65
+ let(:given_headers) do
66
+ {
67
+ 'location' => %w(
68
+ https://target.url:4321/path
69
+ https://target.url:8765/path
70
+ ),
71
+
72
+ 'status' => ["302 Found"],
73
+ 'connection' => ['close'],
74
+ 'transfer-encoding' => ["chunked"]
75
+ }
76
+ end
77
+
78
+ let(:rewritten_status) { subject[0] }
79
+ let(:rewritten_headers) { subject[1] }
80
+ let(:rewritten_body) { subject[2] }
81
+
82
+ specify { expect(rewritten_body).to eq given_body }
83
+ specify { expect(rewritten_status).to eq given_status }
84
+
85
+ specify do
86
+ expect(rewritten_headers).to eq \
87
+ 'location' => %w(http://proxy.url:1234/path http://proxy.url:5678/path)
88
+ end
89
+ end
90
+ end
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-translating_proxy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Mike Nicholaides
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack-proxy
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '0.5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '0.5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.5'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
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
+ description: Proxy that translates strings
56
+ email:
57
+ - mike.nicholaides@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - .rspec
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - lib/rack/translating_proxy.rb
69
+ - rack-translating_proxy.gemspec
70
+ - spec/spec_helper.rb
71
+ - spec/translating_proxy_spec.rb
72
+ homepage: ''
73
+ licenses:
74
+ - MIT
75
+ metadata: {}
76
+ post_install_message:
77
+ rdoc_options: []
78
+ require_paths:
79
+ - lib
80
+ required_ruby_version: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ required_rubygems_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ requirements: []
91
+ rubyforge_project:
92
+ rubygems_version: 2.1.11
93
+ signing_key:
94
+ specification_version: 4
95
+ summary: Proxy that translates strings
96
+ test_files:
97
+ - spec/spec_helper.rb
98
+ - spec/translating_proxy_spec.rb