@@ -32,6 +32,13 @@ def test_3_1
32
32
assert_match ( /Comment/ , c14n )
33
33
c14n = doc . canonicalize ( nil , nil , false )
34
34
refute_match ( /Comment/ , c14n )
35
+
36
+ # with keyword args
37
+ # TODO: when Document#canonicalize is updated to keyword args, these tests should pass
38
+ # c14n = doc.canonicalize(mode: nil, inclusive_namespaces: nil, with_comments: true)
39
+ # assert_match(/Comment/, c14n)
40
+ # c14n = doc.canonicalize(mode: nil, inclusive_namespaces: nil, with_comments: false)
41
+ # refute_match(/Comment/, c14n)
35
42
end
36
43
37
44
def test_exclude_block_params
@@ -199,13 +206,113 @@ def test_c14n_modes
199
206
end
200
207
end
201
208
209
+ def test_c14n_modes_with_keyword_args
210
+ # http://www.w3.org/TR/xml-exc-c14n/#sec-Enveloping
211
+
212
+ doc1 = Nokogiri . XML ( <<~EOXML )
213
+ <n0:local xmlns:n0="http://foobar.org" xmlns:n3="ftp://example.org">
214
+ <n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
215
+ <n3:stuff xmlns:n3="ftp://example.org"/>
216
+ </n1:elem2>
217
+ </n0:local>
218
+ EOXML
219
+ node1 = doc1 . at_xpath ( "//n1:elem2" , { "n1" => "http://example.net" } )
220
+
221
+ doc2 = Nokogiri . XML ( <<~EOXML )
222
+ <n2:pdu xmlns:n1="http://example.com"
223
+ xmlns:n2="http://foo.example"
224
+ xmlns:n4="http://foo.example"
225
+ xml:lang="fr"
226
+ xml:space="retain">
227
+ <n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
228
+ <n3:stuff xmlns:n3="ftp://example.org"/>
229
+ <n4:stuff />
230
+ </n1:elem2>
231
+ </n2:pdu>
232
+ EOXML
233
+ node2 = doc2 . at_xpath ( "//n1:elem2" , { "n1" => "http://example.net" } )
234
+
235
+ expected = <<~EOF . strip
236
+ <n1:elem2 xmlns:n0="http://foobar.org" xmlns:n1="http://example.net" xmlns:n3="ftp://example.org" xml:lang="en">
237
+ <n3:stuff></n3:stuff>
238
+ </n1:elem2>
239
+ EOF
240
+ c14n = node1 . canonicalize
241
+ assert_equal ( expected , c14n )
242
+
243
+ expected = <<~EOF . strip
244
+ <n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xmlns:n4="http://foo.example" xml:lang="en" xml:space="retain">
245
+ <n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
246
+ <n4:stuff></n4:stuff>
247
+ </n1:elem2>
248
+ EOF
249
+ c14n = node2 . canonicalize
250
+ assert_equal ( expected , c14n )
251
+ c14n = node2 . canonicalize ( mode : XML ::XML_C14N_1_0 )
252
+ assert_equal ( expected , c14n )
253
+ assert_raises ( RuntimeError ) do
254
+ node2 . canonicalize ( mode : XML ::XML_C14N_1_0 , inclusive_namespaces : [ "n2" ] )
255
+ end
256
+
257
+ expected = <<~EOF . strip
258
+ <n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
259
+ <n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
260
+ </n1:elem2>
261
+ EOF
262
+ c14n = node1 . canonicalize ( mode : XML ::XML_C14N_EXCLUSIVE_1_0 )
263
+ assert_equal ( expected , c14n )
264
+
265
+ expected = <<~EOF . strip
266
+ <n1:elem2 xmlns:n1="http://example.net" xml:lang="en">
267
+ <n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
268
+ <n4:stuff xmlns:n4="http://foo.example"></n4:stuff>
269
+ </n1:elem2>
270
+ EOF
271
+ c14n = node2 . canonicalize ( mode : XML ::XML_C14N_EXCLUSIVE_1_0 )
272
+ assert_equal ( expected , c14n )
273
+
274
+ expected = <<~EOF . strip
275
+ <n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xml:lang="en">
276
+ <n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
277
+ <n4:stuff xmlns:n4="http://foo.example"></n4:stuff>
278
+ </n1:elem2>
279
+ EOF
280
+ c14n = node2 . canonicalize ( mode : XML ::XML_C14N_EXCLUSIVE_1_0 , inclusive_namespaces : [ "n2" ] )
281
+ assert_equal ( expected , c14n )
282
+
283
+ expected = <<~EOF . strip
284
+ <n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xmlns:n4="http://foo.example" xml:lang="en">
285
+ <n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
286
+ <n4:stuff></n4:stuff>
287
+ </n1:elem2>
288
+ EOF
289
+ c14n = node2 . canonicalize ( mode : XML ::XML_C14N_EXCLUSIVE_1_0 , inclusive_namespaces : [ "n2" , "n4" ] )
290
+ assert_equal ( expected , c14n )
291
+
292
+ expected = <<~EOF . strip
293
+ <n1:elem2 xmlns:n1="http://example.net" xmlns:n2="http://foo.example" xmlns:n4="http://foo.example" xml:lang="en" xml:space="retain">
294
+ <n3:stuff xmlns:n3="ftp://example.org"></n3:stuff>
295
+ <n4:stuff></n4:stuff>
296
+ </n1:elem2>
297
+ EOF
298
+ c14n = node2 . canonicalize ( mode : XML ::XML_C14N_1_1 )
299
+ assert_equal ( expected , c14n )
300
+ assert_raises ( RuntimeError ) do
301
+ node2 . canonicalize ( mode : XML ::XML_C14N_1_1 , inclusive_namespaces : [ "n2" ] )
302
+ end
303
+ end
304
+
202
305
def test_wrong_params
203
306
xml = "<a><b></b></a>"
204
307
doc = Nokogiri . XML ( xml )
205
308
206
309
assert_raises ( TypeError ) { doc . canonicalize ( :wrong_type ) }
207
310
assert_raises ( TypeError ) { doc . canonicalize ( nil , :wrong_type ) }
208
311
doc . canonicalize ( nil , nil , :wrong_type )
312
+
313
+ # with keyword args
314
+ # assert_raises(TypeError) { doc.canonicalize(nil, inclusive_namespaces: :wrong_type) }
315
+ # doc.canonicalize(nil, inclusive_namespaces: nil, with_comments: :wrong_type)
209
316
end
210
317
end
211
318
end
0 commit comments