stretchr 1.0.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.
@@ -0,0 +1,132 @@
1
+ require 'test/unit'
2
+ require 'test_helper.rb'
3
+
4
+ class StretchrTest < Test::Unit::TestCase
5
+
6
+ def test_new_with_missing_fields
7
+ assert_raise Stretchr::MissingAttributeError do
8
+ stretchr = Stretchr::Client.new({})
9
+ end
10
+ assert_raise Stretchr::MissingAttributeError do
11
+ stretchr = Stretchr::Client.new({public_key: "test", project: "project.company"})
12
+ end
13
+ assert_raise Stretchr::MissingAttributeError do
14
+ stretchr = Stretchr::Client.new({private_key: 'ABC123-private', project: "project.company"})
15
+ end
16
+ assert_raise Stretchr::MissingAttributeError do
17
+ stretchr = Stretchr::Client.new({private_key: 'ABC123-private', public_key: "test"})
18
+ end
19
+
20
+ end
21
+
22
+ def test_new_defaults
23
+
24
+ stretchr = test_stretchr_object
25
+ assert_not_nil stretchr.signatory, "stretchr.signatory"
26
+ assert_not_nil stretchr.transporter, "stretchr.transporter"
27
+
28
+ end
29
+
30
+ def test_new_custom_transporter
31
+
32
+ transporter = Object.new
33
+ stretchr = Stretchr::Client.new({transporter: transporter, private_key: 'ABC123-private', public_key: "test", project: "project.company"})
34
+ assert_equal transporter, stretchr.transporter
35
+
36
+ end
37
+
38
+ def test_new_custom_signatory
39
+
40
+ signatory = Object.new
41
+ stretchr = Stretchr::Client.new({signatory: signatory, private_key: 'ABC123-private', public_key: "test", project: "project.company"})
42
+ assert_equal signatory, stretchr.signatory
43
+
44
+ end
45
+
46
+ def test_make_request
47
+
48
+ stretchr = test_stretchr_object
49
+ stretchr.people(123).books
50
+
51
+ stretchr.http_method = :get
52
+
53
+ request = stretchr.generate_request
54
+
55
+ assert_equal true, request.is_a?(Stretchr::Request)
56
+
57
+ assert_equal(stretchr.http_method, request.http_method)
58
+ assert_equal(stretchr.signed_uri, request.signed_uri)
59
+
60
+ end
61
+
62
+ def test_basic_url_generation
63
+ stretchr = test_stretchr_object
64
+ assert_equal URI.parse("http://project.company.stretchr.com/api/v1/people/1/cars").to_s, stretchr.people(1).cars.to_url
65
+ end
66
+
67
+ def test_paging
68
+ stretchr = test_stretchr_object
69
+ stretchr.people.limit(10).skip(10)
70
+ assert_equal true, stretchr.uri.validate_param_value("~limit", "10"), "limit not set"
71
+ assert_equal true, stretchr.uri.validate_param_value("~skip", "10"), "skip not set"
72
+
73
+ stretchr = test_stretchr_object
74
+ stretchr.people.limit(10).page(2)
75
+ assert_equal true, stretchr.uri.validate_param_value("~limit", "10"), "limit not set"
76
+ assert_equal true, stretchr.uri.validate_param_value("~skip", "10"), "skip not set"
77
+ end
78
+
79
+ def test_orders
80
+ stretchr = test_stretchr_object
81
+ stretchr.people.order("-age")
82
+ assert_equal true, stretchr.uri.validate_param_value("~order", "-age")
83
+
84
+ stretchr = test_stretchr_object
85
+ stretchr.people.order("-age,name")
86
+ assert_equal true, stretchr.uri.validate_param_value("~order", "-age,name")
87
+ end
88
+
89
+ def test_configuration_setup
90
+ Stretchr.config do |s|
91
+ s.private_key = "test_private"
92
+ s.public_key = "test_public"
93
+ s.project = "test"
94
+ end
95
+
96
+ assert_equal Stretchr.instance_eval {@configuration.private_key}, "test_private", "Should have setup configuration for the module"
97
+ assert_nothing_raised do
98
+ client = Stretchr::Client.new
99
+ end
100
+
101
+ assert_raise Stretchr::UnknownConfiguration, "Should raise an error when we pass an unknown configuration in" do
102
+ Stretchr.config do |s|
103
+ s.fake_param = "what"
104
+ end
105
+ end
106
+ #FIXME : this is a hack to reset the client!
107
+ Stretchr.instance_eval {@configuration = Stretchr::Configuration.new}
108
+ end
109
+
110
+ def test_client_shouldnt_expect_options
111
+ assert_nothing_raised do
112
+ client = Stretchr::Client.new(nil)
113
+ end
114
+ end
115
+
116
+ def test_client_should_raise_errors
117
+ stretchr = test_stretchr_object
118
+ stretchr.noisy_errors = true
119
+ assert_raises Stretchr::NotFound, "Should have returned not found!" do
120
+ stretchr.transporter.responses << Stretchr::Response.new({json: ({"~s" => 404}).to_json})
121
+ stretchr.get
122
+ end
123
+ end
124
+
125
+ def test_query
126
+ stretchr = test_stretchr_object
127
+ stretchr.where("name" => "ryan", "age" => ">21")
128
+ assert stretchr.uri.validate_param_value(":name", "ryan"), "Should have searched for a name"
129
+ assert stretchr.uri.validate_param_value(":age", ">21"), "Should search for an age"
130
+ end
131
+
132
+ end
@@ -0,0 +1,74 @@
1
+ require "cgi" unless defined? CGI
2
+ require_relative "../lib/stretchr"
3
+
4
+ def test_stretchr_object
5
+ Stretchr::Client.new({transporter: Stretchr::TestTransporter.new, private_key: 'ABC123-private', public_key: "test", project: "project.company"})
6
+ end
7
+
8
+ module Stretchr
9
+ class GenerateResponse
10
+ class << self
11
+ def get_single_response(params = {})
12
+ response = {
13
+ "~s" => params[:status] || 200,
14
+ "~d" => params[:data],
15
+ }
16
+ response["~e"] = params[:errors] if params[:errors]
17
+ response["~x"] = params[:context] if params[:context]
18
+ response["~ch"] = params[:change_info] if params[:change_info]
19
+ response.to_json
20
+ end
21
+
22
+ def get_collection_response(params = {})
23
+ response = {
24
+ "~s" => params[:status] || 200,
25
+ "~d" => {
26
+ "~t" => params[:total] || 10,
27
+ "~c" => params[:in_response] || 0
28
+ },
29
+ }
30
+ if params[:objects]
31
+ response["~d"]["~i"] = params[:objects]
32
+ response["~d"]["~c"] = params[:objects].length
33
+ end
34
+ response["~e"] = params[:errors] if params[:errors]
35
+ response["~x"] = params[:context] if params[:context]
36
+ response["~ch"] = params[:change_info] if params[:change_info]
37
+ response.to_json
38
+ end
39
+
40
+ def post_response(params = {})
41
+ response = {
42
+ "~s" => params[:status] || 200,
43
+ "~ch" => {"~c" => 1, "~u" => 1, "~d" => 0 }
44
+ }
45
+ response["~ch"]["~deltas"] = params[:deltas] if params[:deltas]
46
+ response.to_json
47
+ end
48
+
49
+ def put_response(params = {})
50
+ {
51
+
52
+ }
53
+ end
54
+
55
+ end
56
+ end
57
+ end
58
+
59
+ module URI
60
+
61
+ def get_param(param)
62
+ CGI.parse(CGI.unescape(self.query))[param]
63
+ end
64
+
65
+ def validate_param_value(param, value)
66
+ CGI.parse(CGI.unescape(self.query))[param].include?(value)
67
+ end
68
+
69
+ def validate_param_presence(param)
70
+ return false if self.query == nil
71
+ CGI.parse(CGI.unescape(self.query))[param] == [] ? false : true
72
+ end
73
+
74
+ end
@@ -0,0 +1,187 @@
1
+ require 'test/unit'
2
+ require 'test_helper.rb'
3
+
4
+ class ResourcesTest < Test::Unit::TestCase
5
+ Stretchr.config do |s|
6
+ s.project = "test"
7
+ s.private_key = "test"
8
+ s.public_key = "test"
9
+ end
10
+
11
+ class Account < Stretchr::Resource
12
+ stretchr_config path: "/books/:id", transporter: Stretchr::TestTransporter.new
13
+
14
+ def self.load_response(response)
15
+ self.stretchr_client.transporter.responses << Stretchr::Response.new({json: response})
16
+ end
17
+ end
18
+
19
+ def test_ghost_methods
20
+ resource = Stretchr::Resource.new
21
+ resource.name = "Ryan"
22
+ assert_equal resource.name, "Ryan", "Should be able to create any attributes for a resource"
23
+ end
24
+
25
+ def test_resources_build_clients
26
+ assert_equal Stretchr::Resource.stretchr_client.class, Stretchr::Client, "Client should exist for Resources"
27
+ assert_equal Account.stretchr_client.path, "/books/:id"
28
+ end
29
+
30
+ def test_strip_tildes
31
+ account = Account.new({"~id" => "test"})
32
+ assert_equal "test", account.stretchr_id, "Should have stripped the tilde"
33
+ end
34
+
35
+ def test_can_find_resources
36
+ response = Stretchr::GenerateResponse.get_single_response({status: 200, data: {"~id" => "test", name: "Ryan"}})
37
+ Account.load_response(response)
38
+ account = Account.find({:id => "test"})
39
+ assert_equal Account, account.class, "Should have returned an account object"
40
+ assert_equal "Ryan", account.name, "Should have returned the data attributes as methods"
41
+ end
42
+
43
+ def test_can_get_all_resources
44
+ response = Stretchr::GenerateResponse.get_collection_response({objects: [{name: "Ryan", "~id" => "ryan"}, {name: "Tim", "~id" => "tim"}]})
45
+ Account.load_response(response)
46
+ accounts = Account.all
47
+ assert_equal "/books/", Account.stretchr_client.transporter.requests.last.signed_uri.path.gsub(/\/api\/v[0-9]*/, "") #gsub to trim out the /api/v1 bit
48
+ assert_equal accounts.first.class, Account, "Should have returned an array of account objects"
49
+ assert_equal accounts.first.name, "Ryan", "Should have returned the data for each object"
50
+ end
51
+
52
+ def test_single_from_all
53
+ response = Stretchr::GenerateResponse.get_collection_response({objects: [{name: "Ryan", "~id" => "ryan"}]})
54
+ Account.load_response(response)
55
+ accounts = Account.all
56
+ assert accounts.is_a?(Array), "Should return an array even for a single response"
57
+ end
58
+
59
+ def test_none_from_all
60
+ response = Stretchr::GenerateResponse.get_collection_response
61
+ Account.load_response(response)
62
+ accounts = Account.all
63
+ assert accounts.is_a?(Array), "Should return an array even for zero objects"
64
+ end
65
+
66
+ def test_save_new_object
67
+ account = Account.new
68
+ account.name = "Ryan"
69
+
70
+ response = Stretchr::GenerateResponse.post_response({deltas: {"~id" => "asdf"}})
71
+ Account.load_response(response)
72
+
73
+ account.save
74
+ assert_equal ({name: "Ryan"}).to_json, Account.stretchr_client.transporter.requests.last.body, "Should have set the correct body"
75
+ assert account.stretchr_id != nil, "Should have returned and set a stretchr id"
76
+ end
77
+
78
+ def test_save_new_object_with_id
79
+ account = Account.new
80
+ account.name = "Ryan"
81
+ account.stretchr_id = "ryan"
82
+
83
+ response = Stretchr::GenerateResponse.post_response
84
+ Account.load_response(response)
85
+
86
+ account.save
87
+ assert JSON.parse(Account.stretchr_client.transporter.requests.last.body).has_key?("~id"), "Should have set the stretchr id for me!"
88
+ end
89
+
90
+ def test_save_old_project
91
+ response = Stretchr::GenerateResponse.get_single_response({status: 200, data: {"~id" => "test", name: "Ryan"}})
92
+ Account.load_response(response)
93
+ account = Account.find({id: "test"})
94
+
95
+ account.name = "Ryan"
96
+
97
+ response = Stretchr::GenerateResponse.post_response
98
+ Account.load_response(response)
99
+ account.save
100
+
101
+ assert Account.stretchr_client.transporter.requests.last.http_method == :put, "Should have sent a put to update stretchr object"
102
+
103
+ end
104
+
105
+ def test_create_method
106
+ response = Stretchr::GenerateResponse.post_response({deltas: [{"~id" => "asdf"}]})
107
+ Account.load_response(response)
108
+
109
+ account = Account.create({name: "Ryan"})
110
+ assert_equal "Ryan", account.name, "Should have created the object with the attributes"
111
+ assert_equal "asdf", account.stretchr_id, "Should have given it a stretchr ID from the response"
112
+
113
+ response = Stretchr::GenerateResponse.post_response({deltas: [{"~id" => "ryan"}, {"~id" => "tim"}]})
114
+ Account.load_response(response)
115
+
116
+ accounts = Account.create([{name: "Ryan"}, {name: "Tim"}])
117
+ assert_equal "ryan", accounts.first.stretchr_id, "Should have returned an array of objects"
118
+ assert_equal "tim", accounts[1].stretchr_id, "Second object should work!"
119
+ end
120
+
121
+ def test_create_with_id
122
+ response = Stretchr::GenerateResponse.post_response({deltas: [{"~id" => "asdf"}]})
123
+ Account.load_response(response)
124
+
125
+ account = Account.create({name: "Ryan", stretchr_id: "ryan-id"})
126
+
127
+ assert Account.stretchr_client.transporter.requests.last.body.include?("~id"), "Should have set the ~id for stretchr"
128
+ end
129
+
130
+ def test_not_found
131
+ response = Stretchr::GenerateResponse.get_single_response({status: 404})
132
+ Account.load_response(response)
133
+ account = Account.find({id: "ryan"})
134
+ assert_equal false, account, "Should have returned false if object not found"
135
+ end
136
+
137
+ def test_find_resources
138
+ response = Stretchr::GenerateResponse.get_collection_response({objects: [{name: "Ryan", "~id" => "ryan"}, {name: "Ryan", "~id" => "tim"}]})
139
+ Account.load_response(response)
140
+ accounts = Account.where("name" => "Ryan")
141
+ assert accounts.length == 2, "should have returned two objects"
142
+ assert accounts.first.is_a?(Account), "should have returned an array of account objects"
143
+ assert accounts.first.name == "Ryan", "should have returned editable objects"
144
+ end
145
+
146
+ def test_find_no_resources
147
+ response = Stretchr::GenerateResponse.get_collection_response
148
+ Account.load_response(response)
149
+ accounts = Account.where("name" => "Ryan")
150
+ assert_equal false, accounts, "Should have returned false for no objects!"
151
+ end
152
+
153
+ def test_remove_params_from_path
154
+ response = Stretchr::GenerateResponse.get_collection_response({objects: [], in_response: 0})
155
+ Account.load_response(response)
156
+ accounts = Account.where({id: "test"})
157
+ last_uri = Account.stretchr_client.transporter.requests.last.signed_uri
158
+ assert last_uri.path.include?("/books/test"), "Should have set the path"
159
+ assert !URI.decode(last_uri.query).include?(":id"), "Should not have included id search in query"
160
+ end
161
+
162
+ def test_nil_path_param
163
+ assert_nothing_raised "Nil attributes shouldn't raise error" do
164
+ Stretchr::Resource.instance_eval { prep_path("/accounts/:account_id", {account_id: nil}) }
165
+ end
166
+ end
167
+
168
+ def test_number_for_param
169
+ assert_nothing_raised "Nil attributes shouldn't raise error" do
170
+ Stretchr::Resource.instance_eval { prep_path("/accounts/:account_id", {account_id: 123}) }
171
+ end
172
+ end
173
+
174
+ def test_to_json
175
+ account = Account.new
176
+ account.name = "Ryan"
177
+ account.stretchr_id = "asdf"
178
+ hash = {"name" => "Ryan", "stretchr_id" => "asdf"}
179
+ assert_equal hash, account.to_hash, "Should have returned a hash"
180
+ assert_equal hash.to_json, account.to_json, "Should have returned json"
181
+ end
182
+
183
+ #FIXME : It needs to know when an item already exists and when it's being created for the first time and handle them appropriately
184
+ #FIXME : It needs to be able to throw errors for 404, etc...
185
+ #FIXME : Should test "where" with params in the path as well. It should add them to the path but remove them from the query
186
+
187
+ end
@@ -0,0 +1,67 @@
1
+ require 'test/unit'
2
+ require 'test_helper.rb'
3
+
4
+ class ResourcesTest < Test::Unit::TestCase
5
+
6
+ def test_signed_uri
7
+
8
+ stretchr = test_stretchr_object
9
+ stretchr.people(123).books
10
+
11
+ assert_equal(true, stretchr.signed_uri.validate_param_presence("~sign"), "~sign param expected")
12
+
13
+ end
14
+
15
+ def test_signing
16
+ public_key = "ABC123"
17
+ private_key = "ABC123-private"
18
+ body = "body"
19
+ url = "http://test.stretchr.com/api/v1?:name=!Mat&:name=!Laurie&:age=>20"
20
+
21
+ #as per documentation
22
+ assert_equal true, Stretchr::Signatory.generate_signed_url("get", url, public_key, private_key, body).validate_param_value("~sign", "6c3dc03b3f85c9eb80ed9e4bd21e82f1bbda5b8d"), "URL signature didn't match expected"
23
+ end
24
+
25
+ def test_signing_again
26
+ public_key = "ABC123"
27
+ private_key = "PRIVATE"
28
+ url = "http://localhost:8080/api/v1?:name=!Mat&:name=!Laurie&:age=>20"
29
+
30
+ #as per documentation
31
+ assert_equal "6841830bf612b03864edeebf9b99b7f48a8edf2d", Stretchr::Signatory.generate_signed_url("get", url, public_key, private_key).get_param("~sign").first, "URL signature didn't match expected"
32
+
33
+ end
34
+
35
+ def test_signing_with_no_query
36
+ public_key = "ABC123"
37
+ private_key = "ABC123-private"
38
+ body = "body"
39
+ url = "http://test.stretchr.com/api/v1"
40
+
41
+ #as per documentation
42
+ assert_equal "d5e1dcbba794be7dc6767076bd4747b51837f21d", Stretchr::Signatory.generate_signed_url("get", url, public_key, private_key, body).get_param("~sign").first, "URL signature didn't match expected"
43
+ end
44
+
45
+ def test_signing_with_escapable_characters
46
+
47
+ public_key = "ABC123"
48
+ private_key = "PRIVATE"
49
+ url = "http://localhost:8080/api/v1/people?:~created=>10000000"
50
+
51
+ #as per documentation
52
+ assert_equal "b54b16d3542a1497bf22c4f61aa935e81032e7b5", Stretchr::Signatory.generate_signed_url("get", url, public_key, private_key).get_param("~sign").first, "URL signature didn't match expected"
53
+
54
+ end
55
+
56
+ def test_private_key_and_body_hash_removal
57
+ #we shouldn't see the private key or body hash in the final url
58
+ public_key = "ABC123"
59
+ private_key = "ABC123-private"
60
+ body = "body"
61
+ url = "http://test.stretchr.com/api/v1?:name=!Mat&:name=!Laurie&:age=>20"
62
+
63
+ assert_equal false, Stretchr::Signatory.generate_signed_url("get", url, public_key, private_key, body).validate_param_presence("~private"), "private param included"
64
+ assert_equal false, Stretchr::Signatory.generate_signed_url("get", url, public_key, private_key, body).validate_param_presence("~bodyhash"), "private param included"
65
+ end
66
+
67
+ end
@@ -0,0 +1,190 @@
1
+ require 'test/unit'
2
+ require 'test_helper.rb'
3
+
4
+ class StretchrHttpActionsTest < Test::Unit::TestCase
5
+
6
+ def test_create
7
+
8
+ stretchr = test_stretchr_object
9
+
10
+ test_response = Stretchr::Response.new(:json => '{"~s":200,"~d":{"name":"Ryan"}}')
11
+ stretchr.transporter.responses << test_response
12
+
13
+ response_from_post = stretchr.people(123).create({:name=>"Ryan"})
14
+
15
+ assert_equal(response_from_post, test_response, "response from get should be the response form the transporter")
16
+
17
+ if assert_equal(1, stretchr.transporter.requests.length)
18
+
19
+ request = stretchr.transporter.requests[0]
20
+
21
+ assert_equal(:post, request.http_method, "http_method")
22
+ assert_equal(request.body, {:name=>"Ryan"}.to_json)
23
+ assert_equal("http://project.company.stretchr.com/api/v1/people/123?%7Ekey=test&%7Esign=a2c48a40179fdd9db12f157d289e9ec79b00fe04", request.signed_uri.to_s)
24
+
25
+ end
26
+
27
+ end
28
+
29
+ def test_update
30
+
31
+ stretchr = test_stretchr_object
32
+
33
+ test_response = Stretchr::Response.new(:json => '{"~s":200,"~d":{"name":"Ryan"}}')
34
+ stretchr.transporter.responses << test_response
35
+
36
+ response_from_put = stretchr.people(123).update({:name=>"Mat"})
37
+
38
+ assert_equal(response_from_put, test_response, "response from get should be the response form the transporter")
39
+
40
+ if assert_equal(1, stretchr.transporter.requests.length)
41
+
42
+ request = stretchr.transporter.requests[0]
43
+
44
+ assert_equal(:put, request.http_method, "http_method")
45
+ assert_equal(request.body, {:name=>"Mat"}.to_json)
46
+ assert_equal("http://project.company.stretchr.com/api/v1/people/123?%7Ekey=test&%7Esign=837147f09451a998c041c328821c6b72f88f3336", request.signed_uri.to_s)
47
+
48
+ end
49
+
50
+ end
51
+
52
+ def test_replace
53
+
54
+ stretchr = test_stretchr_object
55
+
56
+ test_response = Stretchr::Response.new(:json => '{"~s":200,"~d":{"name":"Ryan"}}')
57
+ stretchr.transporter.responses << test_response
58
+
59
+ response_from_post = stretchr.people(123).replace({:name=>"Ryan"})
60
+
61
+ assert_equal(response_from_post, test_response, "response from get should be the response form the transporter")
62
+
63
+ if assert_equal(1, stretchr.transporter.requests.length)
64
+
65
+ request = stretchr.transporter.requests[0]
66
+
67
+ assert_equal(:post, request.http_method, "http_method")
68
+ assert_equal(request.body, {:name=>"Ryan"}.to_json)
69
+ assert_equal("http://project.company.stretchr.com/api/v1/people/123?%7Ekey=test&%7Esign=a2c48a40179fdd9db12f157d289e9ec79b00fe04", request.signed_uri.to_s)
70
+
71
+ end
72
+
73
+ end
74
+
75
+ def test_read
76
+
77
+ stretchr = test_stretchr_object
78
+
79
+ test_response = Stretchr::Response.new(:json => '{"~s":200,"~d":{"name":"Ryan"}}')
80
+ stretchr.transporter.responses << test_response
81
+
82
+ response_from_get = stretchr.people(123).read
83
+
84
+ assert_equal(test_response, response_from_get, "response from get should be the response form the transporter")
85
+
86
+ if assert_equal(1, stretchr.transporter.requests.length)
87
+
88
+ request = stretchr.transporter.requests[0]
89
+
90
+ assert_equal(:get, request.http_method, "http_method")
91
+ assert_nil(request.body, "body")
92
+ assert_equal("http://project.company.stretchr.com/api/v1/people/123?%7Ekey=test&%7Esign=a6bfa773f9169dbeae077ba6a8ac3da07fc19db3", request.signed_uri.to_s)
93
+
94
+ end
95
+
96
+ end
97
+
98
+ def test_get
99
+
100
+ stretchr = test_stretchr_object
101
+
102
+ test_response = Stretchr::Response.new(:json => '{"~s":200,"~d":{"name":"Ryan"}}')
103
+ stretchr.transporter.responses << test_response
104
+
105
+ response_from_get = stretchr.people(123).get
106
+
107
+ assert_equal(test_response, response_from_get, "response from get should be the response form the transporter")
108
+
109
+ if assert_equal(1, stretchr.transporter.requests.length)
110
+
111
+ request = stretchr.transporter.requests[0]
112
+
113
+ assert_equal(:get, request.http_method, "http_method")
114
+ assert_nil(request.body, "body")
115
+ assert_equal("http://project.company.stretchr.com/api/v1/people/123?%7Ekey=test&%7Esign=a6bfa773f9169dbeae077ba6a8ac3da07fc19db3", request.signed_uri.to_s)
116
+
117
+ end
118
+
119
+ end
120
+
121
+ def test_post
122
+
123
+ stretchr = test_stretchr_object
124
+
125
+ test_response = Stretchr::Response.new(:json => '{"~s":200,"~d":{"name":"Ryan"}}')
126
+ stretchr.transporter.responses << test_response
127
+
128
+ response_from_post = stretchr.people(123).body({:name=>"Ryan"}).post
129
+
130
+ assert_equal(response_from_post, test_response, "response from get should be the response form the transporter")
131
+
132
+ if assert_equal(1, stretchr.transporter.requests.length)
133
+
134
+ request = stretchr.transporter.requests[0]
135
+
136
+ assert_equal(:post, request.http_method, "http_method")
137
+ assert_equal(request.body, {:name=>"Ryan"}.to_json)
138
+ assert_equal("http://project.company.stretchr.com/api/v1/people/123?%7Ekey=test&%7Esign=a2c48a40179fdd9db12f157d289e9ec79b00fe04", request.signed_uri.to_s)
139
+
140
+ end
141
+
142
+ end
143
+
144
+ def test_put
145
+
146
+ stretchr = test_stretchr_object
147
+
148
+ test_response = Stretchr::Response.new(:json => '{"~s":200,"~d":{"name":"Ryan"}}')
149
+ stretchr.transporter.responses << test_response
150
+
151
+ response_from_put = stretchr.people(123).body({:name=>"Mat"}).put
152
+
153
+ assert_equal(response_from_put, test_response, "response from get should be the response form the transporter")
154
+
155
+ if assert_equal(1, stretchr.transporter.requests.length)
156
+
157
+ request = stretchr.transporter.requests[0]
158
+
159
+ assert_equal(:put, request.http_method, "http_method")
160
+ assert_equal(request.body, {:name=>"Mat"}.to_json)
161
+ assert_equal("http://project.company.stretchr.com/api/v1/people/123?%7Ekey=test&%7Esign=837147f09451a998c041c328821c6b72f88f3336", request.signed_uri.to_s)
162
+
163
+ end
164
+
165
+ end
166
+
167
+ def test_delete
168
+
169
+ stretchr = test_stretchr_object
170
+
171
+ test_response = Stretchr::Response.new(:json => '{"~s":200,"~d":{"name":"Ryan"}}')
172
+ stretchr.transporter.responses << test_response
173
+
174
+ response_from_delete = stretchr.people(123).delete
175
+
176
+ assert_equal(response_from_delete, test_response, "response from get should be the response form the transporter")
177
+
178
+ if assert_equal(1, stretchr.transporter.requests.length)
179
+
180
+ request = stretchr.transporter.requests[0]
181
+
182
+ assert_equal(:delete, request.http_method, "http_method")
183
+ assert_nil(request.body, "body")
184
+ assert_equal("http://project.company.stretchr.com/api/v1/people/123?%7Ekey=test&%7Esign=7becba6ba04d40b7dd19408776f7f028c0d8f864", request.signed_uri.to_s)
185
+
186
+ end
187
+
188
+ end
189
+
190
+ end
@@ -0,0 +1,26 @@
1
+ require 'test/unit'
2
+ require 'test_helper.rb'
3
+
4
+ class StretchrRequestTest < Test::Unit::TestCase
5
+
6
+ def test_new
7
+
8
+ uri = URI.parse("http://test.stretchr.com/api/v1")
9
+ r = Stretchr::Request.new(
10
+ :http_method => :get,
11
+ :signed_uri => uri,
12
+ :body => "This is the body",
13
+ :headers => {
14
+ "X-Custom-Header" => "Hello"
15
+ }
16
+ )
17
+
18
+ assert_equal(r.http_method, :get)
19
+ assert_equal(r.signed_uri, uri)
20
+ assert_equal(r.body, "This is the body")
21
+ assert_equal(r.headers["X-Custom-Header"], "Hello")
22
+
23
+ end
24
+
25
+
26
+ end