kronk 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,258 @@
1
+ require 'test/test_helper'
2
+
3
+ class TestResponse < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @html_resp = Kronk::Request.retrieve_file "test/mocks/200_response.txt"
7
+ @json_resp = Kronk::Request.retrieve_file "test/mocks/200_response.json"
8
+ @plist_resp = Kronk::Request.retrieve_file "test/mocks/200_response.plist"
9
+ @xml_resp = Kronk::Request.retrieve_file "test/mocks/200_response.xml"
10
+ end
11
+
12
+
13
+ def test_read_new
14
+ File.open "test/mocks/200_response.txt", "r" do |file|
15
+ resp = Kronk::Response.read_new file
16
+
17
+
18
+ expected_header = "#{mock_200_response.split("\r\n\r\n", 2)[0]}\r\n"
19
+
20
+ assert Net::HTTPResponse === resp
21
+ assert_equal mock_200_response, resp.raw
22
+ assert_equal expected_header, resp.raw_header
23
+ end
24
+ end
25
+
26
+
27
+ def test_read_raw_from
28
+ resp = mock_200_response
29
+ chunks = [resp[0..123], resp[124..200], resp[201..-1]]
30
+ chunks = chunks.map{|c| "-> #{c.inspect}"}
31
+ str = [chunks[0], "<- reading 123 bytes", chunks[1], chunks[2]].join "\n"
32
+ str = "<- \"mock debug request\"\n#{str}\nread 123 bytes"
33
+
34
+ io = StringIO.new str
35
+
36
+ req, resp, bytes = Kronk::Response.read_raw_from io
37
+
38
+ assert_equal "mock debug request", req
39
+ assert_equal mock_200_response, resp
40
+ assert_equal 123, bytes
41
+ end
42
+
43
+
44
+ def test_parsed_body_json
45
+ raw = File.read "test/mocks/200_response.json"
46
+ expected = JSON.parse raw.split("\r\n\r\n")[1]
47
+
48
+ assert_equal expected, @json_resp.parsed_body
49
+ assert_equal @xml_resp.parsed_body, @json_resp.parsed_body
50
+
51
+ assert_raises RuntimeError do
52
+ @json_resp.parsed_body Kronk::PlistParser
53
+ end
54
+ end
55
+
56
+
57
+ def test_parsed_body_plist
58
+ raw = File.read "test/mocks/200_response.plist"
59
+ expected = Kronk::PlistParser.parse raw.split("\r\n\r\n")[1]
60
+
61
+ assert_equal expected, @plist_resp.parsed_body
62
+ assert_equal @json_resp.parsed_body, @plist_resp.parsed_body
63
+ end
64
+
65
+
66
+ def test_parsed_body_xml
67
+ raw = File.read "test/mocks/200_response.xml"
68
+ expected = Kronk::XMLParser.parse raw.split("\r\n\r\n")[1]
69
+
70
+ assert_equal expected, @xml_resp.parsed_body
71
+ assert_equal @json_resp.parsed_body, @xml_resp.parsed_body
72
+ end
73
+
74
+
75
+ def test_parsed_body_missing_parser
76
+ assert_raises Kronk::Response::MissingParser do
77
+ @html_resp.parsed_body
78
+ end
79
+ end
80
+
81
+
82
+ def test_parsed_body_bad_parser
83
+ assert_raises JSON::ParserError do
84
+ @html_resp.parsed_body JSON
85
+ end
86
+ end
87
+
88
+
89
+ def test_parsed_header
90
+ assert_equal @json_resp.to_hash, @json_resp.parsed_header
91
+
92
+ assert_equal({'content-type' => ["application/json; charset=utf-8"]},
93
+ @json_resp.parsed_header('Content-Type'))
94
+
95
+ assert_equal({'date' => ["Fri, 03 Dec 2010 21:49:00 GMT"],
96
+ 'content-type' => ["application/json; charset=utf-8"]},
97
+ @json_resp.parsed_header(['Content-Type', 'Date']))
98
+
99
+ assert_nil @json_resp.parsed_header(false)
100
+ assert_nil @json_resp.parsed_header(nil)
101
+ end
102
+
103
+
104
+ def test_raw_header
105
+ assert_equal "#{@json_resp.raw.split("\r\n\r\n")[0]}\r\n",
106
+ @json_resp.raw_header
107
+
108
+ assert_equal "Content-Type: application/json; charset=utf-8\r\n",
109
+ @json_resp.raw_header('Content-Type')
110
+
111
+ assert_equal "Date: Fri, 03 Dec 2010 21:49:00 GMT\r\nContent-Type: application/json; charset=utf-8\r\n",
112
+ @json_resp.raw_header(['Content-Type', 'Date'])
113
+
114
+ assert_nil @json_resp.raw_header(false)
115
+ assert_nil @json_resp.raw_header(nil)
116
+ end
117
+
118
+
119
+ def test_selective_string
120
+ body = @json_resp.raw.split("\r\n\r\n")[1]
121
+
122
+ assert_equal body,
123
+ @json_resp.selective_string
124
+ end
125
+
126
+
127
+ def test_selective_string_no_body
128
+ body = @json_resp.raw.split("\r\n\r\n")[1]
129
+
130
+ assert_nil @json_resp.selective_string(:no_body => true)
131
+
132
+ assert_equal "#{@json_resp.raw.split("\r\n\r\n")[0]}\r\n",
133
+ @json_resp.selective_string(:no_body => true,
134
+ :with_headers => true)
135
+ end
136
+
137
+
138
+ def test_selective_string_single_header
139
+ body = @json_resp.raw.split("\r\n\r\n")[1]
140
+
141
+ expected = "Content-Type: application/json; charset=utf-8\r\n\r\n#{body}"
142
+ assert_equal expected,
143
+ @json_resp.selective_string(:with_headers => "Content-Type")
144
+ end
145
+
146
+
147
+ def test_selective_multiple_headers
148
+ body = @json_resp.raw.split("\r\n\r\n")[1]
149
+
150
+ expected = "Date: Fri, 03 Dec 2010 21:49:00 GMT\r\nContent-Type: application/json; charset=utf-8\r\n\r\n#{body}"
151
+ assert_equal expected,
152
+ @json_resp.selective_string(
153
+ :with_headers => ["Content-Type", "Date"])
154
+
155
+ expected = "Date: Fri, 03 Dec 2010 21:49:00 GMT\r\nContent-Type: application/json; charset=utf-8\r\n"
156
+ assert_equal expected,
157
+ @json_resp.selective_string(:no_body => true,
158
+ :with_headers => ["Content-Type", "Date"])
159
+ end
160
+
161
+
162
+ def test_selective_data
163
+ body = JSON.parse @json_resp.body
164
+ head = @json_resp.to_hash
165
+
166
+ assert_equal body, @json_resp.selective_data
167
+
168
+ assert_nil @json_resp.selective_data(:no_body => true)
169
+
170
+ assert_equal "#{@json_resp.raw.split("\r\n\r\n")[0]}\r\n",
171
+ @json_resp.selective_string(:no_body => true,
172
+ :with_headers => true)
173
+ end
174
+
175
+
176
+ def test_selective_data_single_header
177
+ body = JSON.parse @json_resp.body
178
+ expected =
179
+ [{'content-type' => ['application/json; charset=utf-8']}, body]
180
+
181
+ assert_equal expected,
182
+ @json_resp.selective_data(:with_headers => "Content-Type")
183
+ end
184
+
185
+
186
+ def test_selective_data_multiple_headers
187
+ body = JSON.parse @json_resp.body
188
+ expected =
189
+ [{'content-type' => ['application/json; charset=utf-8'],
190
+ 'date' => ["Fri, 03 Dec 2010 21:49:00 GMT"]
191
+ }, body]
192
+
193
+ assert_equal expected,
194
+ @json_resp.selective_data(
195
+ :with_headers => ["Content-Type", "Date"])
196
+ end
197
+
198
+
199
+ def test_selective_data_no_body
200
+ body = JSON.parse @json_resp.body
201
+ expected =
202
+ [{'content-type' => ['application/json; charset=utf-8'],
203
+ 'date' => ["Fri, 03 Dec 2010 21:49:00 GMT"]
204
+ }]
205
+
206
+ assert_equal expected,
207
+ @json_resp.selective_data(:no_body => true,
208
+ :with_headers => ["Content-Type", "Date"])
209
+ end
210
+
211
+
212
+ def test_selective_data_only_data
213
+ expected = {"business" => {"id" => "1234"},
214
+ "original_request"=> {"id"=>"1234"}}
215
+
216
+ assert_equal expected,
217
+ @json_resp.selective_data(:only_data => "**/id")
218
+ end
219
+
220
+
221
+ def test_selective_data_multiple_only_data
222
+ expected = {"business" => {"id" => "1234"},
223
+ "request_id" => "mock_rid"}
224
+
225
+ assert_equal expected,
226
+ @json_resp.selective_data(:only_data => ["business/id", "request_id"])
227
+ end
228
+
229
+
230
+ def test_selective_data_ignore_data
231
+ expected = JSON.parse @json_resp.body
232
+ expected['business'].delete 'id'
233
+ expected['original_request'].delete 'id'
234
+
235
+ assert_equal expected,
236
+ @json_resp.selective_data(:ignore_data => "**/id")
237
+ end
238
+
239
+
240
+ def test_selective_data_multiple_only_data
241
+ expected = JSON.parse @json_resp.body
242
+ expected['business'].delete 'id'
243
+ expected.delete 'request_id'
244
+
245
+ assert_equal expected,
246
+ @json_resp.selective_data(:ignore_data => ["business/id", "request_id"])
247
+ end
248
+
249
+
250
+
251
+ def test_selective_data_collected_and_ignored
252
+ expected = {"business" => {"id" => "1234"}}
253
+
254
+ assert_equal expected,
255
+ @json_resp.selective_data(:only_data => "**/id",
256
+ :ignore_data => "original_request")
257
+ end
258
+ end
@@ -0,0 +1,101 @@
1
+ require 'test/test_helper'
2
+
3
+ class TestXmlParser < Test::Unit::TestCase
4
+
5
+ def setup
6
+ @xml = <<-STR
7
+ <root>
8
+ <tests>
9
+ <test>
10
+ <t>1</t>
11
+ <t>2</t>
12
+ </test>
13
+ <test>2</test>
14
+ <foo>bar</foo>
15
+ </tests>
16
+ <subs>
17
+ <sub>a</sub>
18
+ <sub>b</sub>
19
+ </subs>
20
+ <acks>
21
+ <ack>
22
+ <ack>12</ack>
23
+ <ack>34</ack>
24
+ </ack>
25
+ <ack>
26
+ <ack>56</ack>
27
+ <ack>78</ack>
28
+ </ack>
29
+ </acks>
30
+ </root>
31
+ STR
32
+
33
+ @ary_xml = <<-STR
34
+ <root>
35
+ <tests>
36
+ <test>A1</test>
37
+ <test>A2</test>
38
+ </tests>
39
+ <tests>
40
+ <test>B1</test>
41
+ <test>B2</test>
42
+ </tests>
43
+ <tests>
44
+ <test>C1</test>
45
+ <test>C2</test>
46
+ <test>
47
+ <test>C3a</test>
48
+ <test>C3b</test>
49
+ </test>
50
+ </tests>
51
+ <tests>
52
+ <test>
53
+ <test>D1a
54
+ Content goes here</test>
55
+ <test>D1b</test>
56
+ </test>
57
+ <test>D2</test>
58
+ <tests>
59
+ <test>D3a</test>
60
+ <test>D3b</test>
61
+ </tests>
62
+ </tests>
63
+ </root>
64
+ STR
65
+ end
66
+
67
+
68
+ def test_node_value_arrays
69
+ expected = {
70
+ "root" => [
71
+ ["A1", "A2"],
72
+ ["B1", "B2"],
73
+ ["C1", "C2", ["C3a", "C3b"]],
74
+ {"tests"=>["D3a", "D3b"],
75
+ "test"=>[["D1a\nContent goes here", "D1b"], "D2"]}
76
+ ]
77
+ }
78
+
79
+ root_node = Nokogiri.XML @ary_xml do |config|
80
+ config.default_xml.noblanks
81
+ end
82
+
83
+ results = Kronk::XMLParser.node_value(root_node.children)
84
+ assert_equal expected, results
85
+ end
86
+
87
+
88
+ def test_parse
89
+ data = Kronk::XMLParser.parse @xml
90
+ expected = {
91
+ "acks"=>[["12", "34"], ["56", "78"]],
92
+ "tests"=>{
93
+ "foo"=>"bar",
94
+ "test"=>[["1", "2"], "2"]
95
+ },
96
+ "subs"=>["a", "b"]
97
+ }
98
+
99
+ assert_equal expected, data
100
+ end
101
+ end
metadata ADDED
@@ -0,0 +1,225 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: kronk
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Jeremie Castagna
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-12-06 00:00:00 -08:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: plist
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 3
32
+ - 1
33
+ - 0
34
+ version: 3.1.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: json
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 31
46
+ segments:
47
+ - 1
48
+ - 2
49
+ - 0
50
+ version: 1.2.0
51
+ type: :runtime
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: nokogiri
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 29
62
+ segments:
63
+ - 1
64
+ - 3
65
+ - 3
66
+ version: 1.3.3
67
+ type: :runtime
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ name: i18n
71
+ prerelease: false
72
+ requirement: &id004 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ hash: 11
78
+ segments:
79
+ - 0
80
+ - 5
81
+ - 0
82
+ version: 0.5.0
83
+ type: :runtime
84
+ version_requirements: *id004
85
+ - !ruby/object:Gem::Dependency
86
+ name: activesupport
87
+ prerelease: false
88
+ requirement: &id005 !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ hash: 15
94
+ segments:
95
+ - 2
96
+ - 0
97
+ - 0
98
+ version: 2.0.0
99
+ type: :runtime
100
+ version_requirements: *id005
101
+ - !ruby/object:Gem::Dependency
102
+ name: rubyforge
103
+ prerelease: false
104
+ requirement: &id006 !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ hash: 7
110
+ segments:
111
+ - 2
112
+ - 0
113
+ - 4
114
+ version: 2.0.4
115
+ type: :development
116
+ version_requirements: *id006
117
+ - !ruby/object:Gem::Dependency
118
+ name: mocha
119
+ prerelease: false
120
+ requirement: &id007 !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ hash: 47
126
+ segments:
127
+ - 0
128
+ - 9
129
+ - 10
130
+ version: 0.9.10
131
+ type: :development
132
+ version_requirements: *id007
133
+ - !ruby/object:Gem::Dependency
134
+ name: hoe
135
+ prerelease: false
136
+ requirement: &id008 !ruby/object:Gem::Requirement
137
+ none: false
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ hash: 19
142
+ segments:
143
+ - 2
144
+ - 6
145
+ - 2
146
+ version: 2.6.2
147
+ type: :development
148
+ version_requirements: *id008
149
+ description: |-
150
+ Kronk runs diffs against data from live and cached http responses.
151
+ Kronk was made possible by the sponsoring of AT&T Interactive.
152
+ email:
153
+ - yaksnrainbows@gmail.com
154
+ executables:
155
+ - kronk
156
+ extensions: []
157
+
158
+ extra_rdoc_files:
159
+ - History.txt
160
+ - Manifest.txt
161
+ - README.txt
162
+ files:
163
+ - .autotest
164
+ - History.txt
165
+ - Manifest.txt
166
+ - README.txt
167
+ - Rakefile
168
+ - bin/kronk
169
+ - lib/kronk.rb
170
+ - lib/kronk/data_set.rb
171
+ - lib/kronk/diff.rb
172
+ - lib/kronk/plist_parser.rb
173
+ - lib/kronk/request.rb
174
+ - lib/kronk/response.rb
175
+ - lib/kronk/xml_parser.rb
176
+ - test/test_kronk.rb
177
+ - test/test_data_set.rb
178
+ - test/test_diff.rb
179
+ - test/test_helper.rb
180
+ - test/test_request.rb
181
+ - test/test_response.rb
182
+ - test/test_xml_parser.rb
183
+ has_rdoc: true
184
+ homepage: https://github.com/yaksnrainbows/kronk
185
+ licenses: []
186
+
187
+ post_install_message:
188
+ rdoc_options:
189
+ - --main
190
+ - README.txt
191
+ require_paths:
192
+ - lib
193
+ required_ruby_version: !ruby/object:Gem::Requirement
194
+ none: false
195
+ requirements:
196
+ - - ">="
197
+ - !ruby/object:Gem::Version
198
+ hash: 3
199
+ segments:
200
+ - 0
201
+ version: "0"
202
+ required_rubygems_version: !ruby/object:Gem::Requirement
203
+ none: false
204
+ requirements:
205
+ - - ">="
206
+ - !ruby/object:Gem::Version
207
+ hash: 3
208
+ segments:
209
+ - 0
210
+ version: "0"
211
+ requirements: []
212
+
213
+ rubyforge_project: kronk
214
+ rubygems_version: 1.3.7
215
+ signing_key:
216
+ specification_version: 3
217
+ summary: Kronk runs diffs against data from live and cached http responses
218
+ test_files:
219
+ - test/test_data_set.rb
220
+ - test/test_diff.rb
221
+ - test/test_helper.rb
222
+ - test/test_kronk.rb
223
+ - test/test_request.rb
224
+ - test/test_response.rb
225
+ - test/test_xml_parser.rb