servme 0.0.1

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.
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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in servme.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Test Double
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,67 @@
1
+ # Servme
2
+
3
+ Servme is very rough and not ready for public consumption.
4
+
5
+ If you're still reading, servme is a test library that lets you replace some server that your application depends on with an easy-to-stub Sinatra app that can be run in a thread that's subordinate to your tests.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your Gemfile's test group:
10
+
11
+ gem 'servme'
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install servme
20
+
21
+ ## Usage
22
+
23
+ In your spec helper:
24
+
25
+ ``` ruby
26
+ Thread.new { Servme.start(:port => 12345) }
27
+ RSpec.configure do |config|
28
+ config.include Servme::DSL
29
+ config.after(:each) do
30
+ Servme.reset
31
+ end
32
+ end
33
+ ```
34
+
35
+ Now, the following DSL is going to be entirely gutted ASAP, but for now, in your specs:
36
+
37
+ ``` ruby
38
+ before(:each) do
39
+ on({
40
+ :url => "/api/login",
41
+ :method => :post,
42
+ :params => {
43
+ :login => "todd",
44
+ :password => "scotch"
45
+ }
46
+ }).respond_with(:token => "1234567890") }
47
+ end
48
+ ```
49
+
50
+ And POSTs to "/api/login" with login "todd" and password "scotch" will get a JSON response of `{"token": "1234567890"}`.
51
+
52
+ If you want to trigger a certain status code, you can do this:
53
+
54
+ ``` ruby
55
+ before(:each) do
56
+ on({
57
+ :url => "/api/login",
58
+ :method => :post,
59
+ :params => {
60
+ :login => "todd",
61
+ :password => "scotch"
62
+ }
63
+ }).error_with(401) }
64
+ end
65
+ ```
66
+
67
+ All other requests will send back JSON including the request params with a 404 code.
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/servme.rb ADDED
@@ -0,0 +1,4 @@
1
+ require "servme/version"
2
+ require 'servme/service'
3
+ require 'servme/service_stubbing'
4
+ require 'servme/dsl'
data/lib/servme/dsl.rb ADDED
@@ -0,0 +1,19 @@
1
+ module Servme
2
+ module DSL
3
+ def on(request)
4
+ ServiceStubbing.new(request)
5
+ end
6
+ end
7
+
8
+ #methods invoked statically Servme.foo_bar
9
+ def self.start(options = {})
10
+ Service.run!({
11
+ :port => 51922
12
+ }.merge(options))
13
+ end
14
+
15
+ def self.reset
16
+ Service.clear
17
+ end
18
+
19
+ end
@@ -0,0 +1,86 @@
1
+ require 'thin'
2
+ require 'sinatra/base'
3
+ require 'json'
4
+
5
+ module Servme
6
+
7
+ class Service < Sinatra::Base
8
+ set :server, 'thin'
9
+
10
+ get '*' do
11
+ handle_request(:get)
12
+ end
13
+
14
+ post '*' do
15
+ handle_request(:post)
16
+ end
17
+
18
+ def handle_request(type = :get)
19
+ format_response(
20
+ self.class.satisfy(
21
+ request.path,
22
+ type,
23
+ request.send(type.to_s.upcase)
24
+ )
25
+ )
26
+ end
27
+
28
+ def format_response(response)
29
+ body = json?(response) ? JSON::dump(response[:data]) : response[:data]
30
+ [response[:status_code], response[:headers], JSON::dump(response[:data])]
31
+ end
32
+
33
+ def json?(response)
34
+ response[:headers]['Content-Type'] == 'application/json'
35
+ end
36
+
37
+ @@stubbings = {}
38
+
39
+ def self.clear
40
+ @@stubbings = {}
41
+ end
42
+
43
+
44
+ DEFAULT_HEADERS = {
45
+ 'Content-Type' => 'application/json'
46
+ }
47
+
48
+ def self.stub(config)
49
+ (@@stubbings[config[:url]] ||= {}).tap do |urls|
50
+ (urls[config[:method] || :get] ||= {}).tap do |methods|
51
+ methods[stringify_keys(config[:params]) || {}] = {
52
+ :data => config[:response],
53
+ :headers => DEFAULT_HEADERS.merge(config[:headers] || {}),
54
+ :status_code => config[:status_code] || 200
55
+ }
56
+ end
57
+ end
58
+ end
59
+
60
+ def self.satisfy(path, method, params)
61
+ @@stubbings[path][method][params] || default_response(path, method, params)
62
+ rescue
63
+ default_response(path, method, params)
64
+ end
65
+
66
+ def self.default_response(path, method, params)
67
+ {
68
+ :headers => DEFAULT_HEADERS,
69
+ :status_code => 404,
70
+ :data => {
71
+ :error => "Servme doesn't know how to respond to this request",
72
+ :request => {
73
+ :method => method,
74
+ :params => params,
75
+ :path => path
76
+ }
77
+ }
78
+ }
79
+ end
80
+
81
+ def self.stringify_keys(params)
82
+ Hash[params.map {|(k,v)| [k.to_s, v]}]
83
+ end
84
+ end
85
+
86
+ end
@@ -0,0 +1,15 @@
1
+ module Servme
2
+ class ServiceStubbing
3
+ def initialize(request)
4
+ @request = request
5
+ end
6
+
7
+ def respond_with(response)
8
+ Service.stub(@request.merge({ :response => response }))
9
+ end
10
+
11
+ def error_with(status_code)
12
+ Service.stub(@request.merge({ :status_code => status_code }))
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module Servme
2
+ VERSION = "0.0.1"
3
+ end
data/servme.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'servme/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "servme"
8
+ gem.version = Servme::VERSION
9
+ gem.authors = ["Justin Searls"]
10
+ gem.email = ["searls@gmail.com"]
11
+ gem.description = "a simple test server for stubbing API responses"
12
+ gem.summary = "Servme lets you stub server responses by standing-in for some remote system your code under test depends on"
13
+ gem.homepage = "https://github.com/testdouble/servme"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+ ["json","sinatra", "thin"].each { |d| gem.add_runtime_dependency d }
21
+ # ["debugger"].each { |d| gem.add_development_dependency d }
22
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: servme
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Justin Searls
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-10-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: json
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: sinatra
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: thin
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: a simple test server for stubbing API responses
63
+ email:
64
+ - searls@gmail.com
65
+ executables: []
66
+ extensions: []
67
+ extra_rdoc_files: []
68
+ files:
69
+ - .gitignore
70
+ - Gemfile
71
+ - LICENSE.txt
72
+ - README.md
73
+ - Rakefile
74
+ - lib/servme.rb
75
+ - lib/servme/dsl.rb
76
+ - lib/servme/service.rb
77
+ - lib/servme/service_stubbing.rb
78
+ - lib/servme/version.rb
79
+ - servme.gemspec
80
+ homepage: https://github.com/testdouble/servme
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ none: false
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ none: false
94
+ requirements:
95
+ - - ! '>='
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 1.8.24
101
+ signing_key:
102
+ specification_version: 3
103
+ summary: Servme lets you stub server responses by standing-in for some remote system
104
+ your code under test depends on
105
+ test_files: []