forked from savonrb/nori
/
nori_spec.rb
641 lines (549 loc) · 21.6 KB
/
nori_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
require "spec_helper"
describe Nori do
Nori::Parser::PARSERS.each do |parser, class_name|
context "using the :#{parser} parser" do
let(:parser) { parser }
it "should transform a simple tag with content" do
xml = "<tag>This is the contents</tag>"
parse(xml).should == { 'tag' => 'This is the contents' }
end
it "should work with cdata tags" do
xml = <<-END
<tag>
<![CDATA[
text inside cdata
]]>
</tag>
END
parse(xml)["tag"].strip.should == "text inside cdata"
end
it "should transform a simple tag with attributes" do
xml = "<tag attr1='1' attr2='2'></tag>"
hash = { 'tag' => { '@attr1' => '1', '@attr2' => '2' } }
parse(xml).should == hash
end
it "should transform repeating siblings into an array" do
xml =<<-XML
<opt>
<user login="grep" fullname="Gary R Epstein" />
<user login="stty" fullname="Simon T Tyson" />
</opt>
XML
parse(xml)['opt']['user'].class.should == Array
hash = {
'opt' => {
'user' => [{
'@login' => 'grep',
'@fullname' => 'Gary R Epstein'
},{
'@login' => 'stty',
'@fullname' => 'Simon T Tyson'
}]
}
}
parse(xml).should == hash
end
it "should not transform non-repeating siblings into an array" do
xml =<<-XML
<opt>
<user login="grep" fullname="Gary R Epstein" />
</opt>
XML
parse(xml)['opt']['user'].class.should == Hash
hash = {
'opt' => {
'user' => {
'@login' => 'grep',
'@fullname' => 'Gary R Epstein'
}
}
}
parse(xml).should == hash
end
it "should prefix attributes with an @-sign to avoid problems with overwritten values" do
xml =<<-XML
<multiRef id="id1">
<login>grep</login>
<id>76737</id>
</multiRef>
XML
parse(xml)["multiRef"].should == { "login" => "grep", "@id" => "id1", "id" => "76737" }
end
context "without advanced typecasting" do
around do |example|
Nori.advanced_typecasting = false
example.run
Nori.advanced_typecasting = true
end
it "should not transform 'true'" do
parse("<value>true</value>")["value"].should == "true"
end
it "should not transform 'false'" do
parse("<value>false</value>")["value"].should == "false"
end
it "should not transform Strings matching the xs:time format" do
parse("<value>09:33:55Z</value>")["value"].should == "09:33:55Z"
end
it "should not transform Strings matching the xs:date format" do
parse("<value>1955-04-18-05:00</value>")["value"].should == "1955-04-18-05:00"
end
it "should not transform Strings matching the xs:dateTime format" do
parse("<value>1955-04-18T11:22:33-05:00</value>")["value"].should == "1955-04-18T11:22:33-05:00"
end
end
context "with advanced typecasting" do
around do |example|
Nori.advanced_typecasting = true
example.run
Nori.advanced_typecasting = false
end
it "should transform 'true' to TrueClass" do
parse("<value>true</value>")["value"].should == true
end
it "should transform 'false' to FalseClass" do
parse("<value>false</value>")["value"].should == false
end
it "should transform Strings matching the xs:time format to Time objects" do
parse("<value>09:33:55Z</value>")["value"].should == Time.parse("09:33:55Z")
end
it "should transform Strings matching the xs:date format to Date objects" do
parse("<value>1955-04-18-05:00</value>")["value"].should == Date.parse("1955-04-18-05:00")
end
it "should transform Strings matching the xs:dateTime format to DateTime objects" do
parse("<value>1955-04-18T11:22:33-05:00</value>")["value"].should ==
DateTime.parse("1955-04-18T11:22:33-05:00")
end
it "should not transform Strings containing an xs:time String and more" do
parse("<value>09:33:55Z is a time</value>")["value"].should == "09:33:55Z is a time"
end
it "should not transform Strings containing an xs:date String and more" do
parse("<value>1955-04-18-05:00 is a date</value>")["value"].should == "1955-04-18-05:00 is a date"
end
it "should not transform Strings containing an xs:dateTime String and more" do
parse("<value>1955-04-18T11:22:33-05:00 is a dateTime</value>")["value"].should ==
"1955-04-18T11:22:33-05:00 is a dateTime"
end
end
context "Parsing xml with text and attributes" do
before do
xml =<<-XML
<opt>
<user login="grep">Gary R Epstein</user>
<user>Simon T Tyson</user>
</opt>
XML
@data = parse(xml)
end
it "correctly parse text nodes" do
@data.should == {
'opt' => {
'user' => [
'Gary R Epstein',
'Simon T Tyson'
]
}
}
end
it "be parse attributes for text node if present" do
@data['opt']['user'][0].attributes.should == {'login' => 'grep'}
end
it "default attributes to empty hash if not present" do
@data['opt']['user'][1].attributes.should == {}
end
it "add 'attributes' accessor methods to parsed instances of String" do
@data['opt']['user'][0].should respond_to(:attributes)
@data['opt']['user'][0].should respond_to(:attributes=)
end
it "not add 'attributes' accessor methods to all instances of String" do
"some-string".should_not respond_to(:attributes)
"some-string".should_not respond_to(:attributes=)
end
end
it "should typecast an integer" do
xml = "<tag type='integer'>10</tag>"
parse(xml)['tag'].should == 10
end
it "should typecast a true boolean" do
xml = "<tag type='boolean'>true</tag>"
parse(xml)['tag'].should be(true)
end
it "should typecast a false boolean" do
["false"].each do |w|
parse("<tag type='boolean'>#{w}</tag>")['tag'].should be(false)
end
end
it "should typecast a datetime" do
xml = "<tag type='datetime'>2007-12-31 10:32</tag>"
parse(xml)['tag'].should == Time.parse( '2007-12-31 10:32' ).utc
end
it "should typecast a date" do
xml = "<tag type='date'>2007-12-31</tag>"
parse(xml)['tag'].should == Date.parse('2007-12-31')
end
xml_entities = {
"<" => "<",
">" => ">",
'"' => """,
"'" => "'",
"&" => "&"
}
it "should unescape html entities" do
xml_entities.each do |k,v|
xml = "<tag>Some content #{v}</tag>"
parse(xml)['tag'].should =~ Regexp.new(k)
end
end
it "should unescape XML entities in attributes" do
xml_entities.each do |key, value|
xml = "<tag attr='Some content #{value}'></tag>"
parse(xml)['tag']['@attr'].should =~ Regexp.new(key)
end
end
it "should undasherize keys as tags" do
xml = "<tag-1>Stuff</tag-1>"
parse(xml).keys.should include('tag_1')
end
it "should undasherize keys as attributes" do
xml = "<tag1 attr-1='1'></tag1>"
parse(xml)['tag1'].keys.should include('@attr_1')
end
it "should undasherize keys as tags and attributes" do
xml = "<tag-1 attr-1='1'></tag-1>"
parse(xml).keys.should include('tag_1')
parse(xml)['tag_1'].keys.should include('@attr_1')
end
context "with strip_namespaces set to true" do
around do |example|
Nori.strip_namespaces = true
example.run
Nori.strip_namespaces = false
end
it "should strip the namespace from every tag" do
xml = '<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"></soap:Envelope>'
parse(xml).should have_key("Envelope")
end
it "converts namespaced entries to array elements" do
xml = <<-XML
<history
xmlns:ns10="http://ns10.example.com"
xmlns:ns11="http://ns10.example.com">
<ns10:case><ns10:name>a_name</ns10:name></ns10:case>
<ns11:case><ns11:name>another_name</ns11:name></ns11:case>
</history>
XML
expected_case = [{ "name" => "a_name" }, { "name" => "another_name" }]
parse(xml)["history"]["case"].should == expected_case
end
end
context "with convert_tags_to set to a custom formula" do
around do |example|
Nori.convert_tags_to { |tag| tag.snakecase.to_sym }
example.run
Nori.convert_tags_to(nil)
end
it "transforms the tags to snakecase Symbols" do
xml = '<userResponse id="1"><accountStatus>active</accountStatus></userResponse>'
parse(xml).should == { :user_response => { :@id => "1", :account_status => "active" } }
end
end
it "should render nested content correctly" do
xml = "<root><tag1>Tag1 Content <em><strong>This is strong</strong></em></tag1></root>"
parse(xml)['root']['tag1'].should == "Tag1 Content <em><strong>This is strong</strong></em>"
end
it "should render nested content with splshould text nodes correctly" do
xml = "<root>Tag1 Content<em>Stuff</em> Hi There</root>"
parse(xml)['root'].should == "Tag1 Content<em>Stuff</em> Hi There"
end
it "should ignore attributes when a child is a text node" do
xml = "<root attr1='1'>Stuff</root>"
parse(xml).should == { "root" => "Stuff" }
end
it "should ignore attributes when any child is a text node" do
xml = "<root attr1='1'>Stuff <em>in italics</em></root>"
parse(xml).should == { "root" => "Stuff <em>in italics</em>" }
end
it "should correctly transform multiple children" do
xml = <<-XML
<user gender='m'>
<age type='integer'>35</age>
<name>Home Simpson</name>
<dob type='date'>1988-01-01</dob>
<joined-at type='datetime'>2000-04-28 23:01</joined-at>
<is-cool type='boolean'>true</is-cool>
</user>
XML
hash = {
"user" => {
"@gender" => "m",
"age" => 35,
"name" => "Home Simpson",
"dob" => Date.parse('1988-01-01'),
"joined_at" => Time.parse("2000-04-28 23:01"),
"is_cool" => true
}
}
parse(xml).should == hash
end
it "should properly handle nil values (ActiveSupport Compatible)" do
topic_xml = <<-EOT
<topic>
<title></title>
<id type="integer"></id>
<approved type="boolean"></approved>
<written-on type="date"></written-on>
<viewed-at type="datetime"></viewed-at>
<content type="yaml"></content>
<parent-id></parent-id>
<nil_true nil="true"/>
<namespaced xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
</topic>
EOT
expected_topic_hash = {
'title' => nil,
'id' => nil,
'approved' => nil,
'written_on' => nil,
'viewed_at' => nil,
'content' => nil,
'parent_id' => nil,
'nil_true' => nil,
'namespaced' => nil
}
parse(topic_xml)["topic"].should == expected_topic_hash
end
it "should handle a single record from xml (ActiveSupport Compatible)" do
topic_xml = <<-EOT
<topic>
<title>The First Topic</title>
<author-name>David</author-name>
<id type="integer">1</id>
<approved type="boolean"> true </approved>
<replies-count type="integer">0</replies-count>
<replies-close-in type="integer">2592000000</replies-close-in>
<written-on type="date">2003-07-16</written-on>
<viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
<content type="yaml">--- \n1: should be an integer\n:message: Have a nice day\narray: \n- should-have-dashes: true\n should_have_underscores: true\n</content>
<author-email-address>david@loudthinking.com</author-email-address>
<parent-id></parent-id>
<ad-revenue type="decimal">1.5</ad-revenue>
<optimum-viewing-angle type="float">135</optimum-viewing-angle>
<resident type="symbol">yes</resident>
</topic>
EOT
expected_topic_hash = {
'title' => "The First Topic",
'author_name' => "David",
'id' => 1,
'approved' => true,
'replies_count' => 0,
'replies_close_in' => 2592000000,
'written_on' => Date.new(2003, 7, 16),
'viewed_at' => Time.utc(2003, 7, 16, 9, 28),
# Changed this line where the key is :message. The yaml specifies this as a symbol, and who am I to change what you specify
# The line in ActiveSupport is
# 'content' => { 'message' => "Have a nice day", 1 => "should be an integer", "array" => [{ "should-have-dashes" => true, "should_have_underscores" => true }] },
'content' => { :message => "Have a nice day", 1 => "should be an integer", "array" => [{ "should-have-dashes" => true, "should_have_underscores" => true }] },
'author_email_address' => "david@loudthinking.com",
'parent_id' => nil,
'ad_revenue' => BigDecimal("1.50"),
'optimum_viewing_angle' => 135.0,
'resident' => :yes
}
parse(topic_xml)["topic"].each do |k,v|
v.should == expected_topic_hash[k]
end
end
it "should handle multiple records (ActiveSupport Compatible)" do
topics_xml = <<-EOT
<topics type="array">
<topic>
<title>The First Topic</title>
<author-name>David</author-name>
<id type="integer">1</id>
<approved type="boolean">false</approved>
<replies-count type="integer">0</replies-count>
<replies-close-in type="integer">2592000000</replies-close-in>
<written-on type="date">2003-07-16</written-on>
<viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
<content>Have a nice day</content>
<author-email-address>david@loudthinking.com</author-email-address>
<parent-id nil="true"></parent-id>
</topic>
<topic>
<title>The Second Topic</title>
<author-name>Jason</author-name>
<id type="integer">1</id>
<approved type="boolean">false</approved>
<replies-count type="integer">0</replies-count>
<replies-close-in type="integer">2592000000</replies-close-in>
<written-on type="date">2003-07-16</written-on>
<viewed-at type="datetime">2003-07-16T09:28:00+0000</viewed-at>
<content>Have a nice day</content>
<author-email-address>david@loudthinking.com</author-email-address>
<parent-id></parent-id>
</topic>
</topics>
EOT
expected_topic_hash = {
'title' => "The First Topic",
'author_name' => "David",
'id' => 1,
'approved' => false,
'replies_count' => 0,
'replies_close_in' => 2592000000,
'written_on' => Date.new(2003, 7, 16),
'viewed_at' => Time.utc(2003, 7, 16, 9, 28),
'content' => "Have a nice day",
'author_email_address' => "david@loudthinking.com",
'parent_id' => nil
}
# puts Nori.parse(topics_xml)['topics'].first.inspect
parse(topics_xml)["topics"].first.each do |k,v|
v.should == expected_topic_hash[k]
end
end
it "should handle a single record from_xml with attributes other than type (ActiveSupport Compatible)" do
topic_xml = <<-EOT
<rsp stat="ok">
<photos page="1" pages="1" perpage="100" total="16">
<photo id="175756086" owner="55569174@N00" secret="0279bf37a1" server="76" title="Colored Pencil PhotoBooth Fun" ispublic="1" isfriend="0" isfamily="0"/>
</photos>
</rsp>
EOT
expected_topic_hash = {
'@id' => "175756086",
'@owner' => "55569174@N00",
'@secret' => "0279bf37a1",
'@server' => "76",
'@title' => "Colored Pencil PhotoBooth Fun",
'@ispublic' => "1",
'@isfriend' => "0",
'@isfamily' => "0",
}
parse(topic_xml)["rsp"]["photos"]["photo"].each do |k, v|
v.should == expected_topic_hash[k]
end
end
it "should handle an emtpy array (ActiveSupport Compatible)" do
blog_xml = <<-XML
<blog>
<posts type="array"></posts>
</blog>
XML
expected_blog_hash = {"blog" => {"posts" => []}}
parse(blog_xml).should == expected_blog_hash
end
it "should handle empty array with whitespace from xml (ActiveSupport Compatible)" do
blog_xml = <<-XML
<blog>
<posts type="array">
</posts>
</blog>
XML
expected_blog_hash = {"blog" => {"posts" => []}}
parse(blog_xml).should == expected_blog_hash
end
it "should handle array with one entry from_xml (ActiveSupport Compatible)" do
blog_xml = <<-XML
<blog>
<posts type="array">
<post>a post</post>
</posts>
</blog>
XML
expected_blog_hash = {"blog" => {"posts" => ["a post"]}}
parse(blog_xml).should == expected_blog_hash
end
it "should handle array with multiple entries from xml (ActiveSupport Compatible)" do
blog_xml = <<-XML
<blog>
<posts type="array">
<post>a post</post>
<post>another post</post>
</posts>
</blog>
XML
expected_blog_hash = {"blog" => {"posts" => ["a post", "another post"]}}
parse(blog_xml).should == expected_blog_hash
end
it "should handle file types (ActiveSupport Compatible)" do
blog_xml = <<-XML
<blog>
<logo type="file" name="logo.png" content_type="image/png">
</logo>
</blog>
XML
hash = parse(blog_xml)
hash.keys.should include('blog')
hash['blog'].keys.should include('logo')
file = hash['blog']['logo']
file.original_filename.should == 'logo.png'
file.content_type.should == 'image/png'
end
it "should handle file from xml with defaults (ActiveSupport Compatible)" do
blog_xml = <<-XML
<blog>
<logo type="file">
</logo>
</blog>
XML
file = parse(blog_xml)['blog']['logo']
file.original_filename.should == 'untitled'
file.content_type.should == 'application/octet-stream'
end
it "should handle xsd like types from xml (ActiveSupport Compatible)" do
bacon_xml = <<-EOT
<bacon>
<weight type="double">0.5</weight>
<price type="decimal">12.50</price>
<chunky type="boolean"> 1 </chunky>
<expires-at type="dateTime">2007-12-25T12:34:56+0000</expires-at>
<notes type="string"></notes>
<illustration type="base64Binary">YmFiZS5wbmc=</illustration>
</bacon>
EOT
expected_bacon_hash = {
'weight' => 0.5,
'chunky' => true,
'price' => BigDecimal("12.50"),
'expires_at' => Time.utc(2007,12,25,12,34,56),
'notes' => "",
'illustration' => "babe.png"
}
parse(bacon_xml)["bacon"].should == expected_bacon_hash
end
it "should let type trickle through when unknown (ActiveSupport Compatible)" do
product_xml = <<-EOT
<product>
<weight type="double">0.5</weight>
<image type="ProductImage"><filename>image.gif</filename></image>
</product>
EOT
expected_product_hash = {
'weight' => 0.5,
'image' => {'@type' => 'ProductImage', 'filename' => 'image.gif' },
}
parse(product_xml)["product"].should == expected_product_hash
end
it "should handle unescaping from xml (ActiveResource Compatible)" #do
# xml_string = '<person><bare-string>First & Last Name</bare-string><pre-escaped-string>First &amp; Last Name</pre-escaped-string></person>'
# expected_hash = {
# 'bare_string' => 'First & Last Name',
# 'pre_escaped_string' => 'First & Last Name'
# }
#
# parse(xml_string)['person'].should == expected_hash
# end
it "handle an empty xml string" do
parse('').should == {}
end
# As returned in the response body by the unfuddle XML API when creating objects
it "handle an xml string containing a single space" do
parse(' ').should == {}
end
end
end
def parse(xml)
Nori.parse xml, parser
end
end