-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsu_id_test.go
90 lines (82 loc) · 2.08 KB
/
wsu_id_test.go
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
package xmlsecurity
import (
"testing"
"github.com/beevik/etree"
"github.com/deb-ict/go-xml"
)
func Test_GetWsuId(t *testing.T) {
// Create test case
testCase := []struct {
xml string
id string
}{
{
xml: `<Test xmlns:wsu="` + WsuNamespace + `" wsu:Id="123"/>`,
id: "123",
},
{
xml: `<Test xmlns:wsu="` + WsuNamespace + `" Id="123"/>`,
id: "",
},
}
for _, tc := range testCase {
// Prepare the test case
testCaseDocument := etree.NewDocument()
err := testCaseDocument.ReadFromString(tc.xml)
if err != nil {
t.Fatal(err)
}
testCaseContext := xml.NewContext(testCaseDocument)
testCaseContext.SetNamespacePrefix("wsu", WsuNamespace)
// Get test case wsu:Id
wsuId := GetWsuId(testCaseContext, testCaseDocument.Root())
if wsuId != tc.id {
t.Fatal(wsuId)
}
}
}
func Test_SetWsuId(t *testing.T) {
// Create test case
testCase := []struct {
newId string
originalXml string
expectedXml string
}{
{
newId: "123",
originalXml: `<Test xmlns:wsu="` + WsuNamespace + `"/>`,
expectedXml: `<Test xmlns:wsu="` + WsuNamespace + `" wsu:Id="123"/>`,
},
{
newId: "123",
originalXml: `<Test xmlns:wsu="` + WsuNamespace + `" wsu:Id="456"/>`,
expectedXml: `<Test xmlns:wsu="` + WsuNamespace + `" wsu:Id="123"/>`,
},
{
newId: "123",
originalXml: `<Test xmlns:wsu="` + WsuNamespace + `" Id="456"/>`,
expectedXml: `<Test xmlns:wsu="` + WsuNamespace + `" Id="456" wsu:Id="123"/>`,
},
}
for _, tc := range testCase {
// Prepare the test case
testCaseDocument := etree.NewDocument()
err := testCaseDocument.ReadFromString(tc.originalXml)
if err != nil {
t.Fatal(err)
}
testCaseContext := xml.NewContext(testCaseDocument)
testCaseContext.SetNamespacePrefix("wsu", WsuNamespace)
// Set test case wsu:Id
if tc.newId != "" {
SetWsuId(testCaseContext, testCaseDocument.Root(), tc.newId)
}
resultXml, err := testCaseDocument.WriteToString()
if err != nil {
t.Fatal(err)
}
if resultXml != tc.expectedXml {
t.Fatalf("resultXml = %s; want %s", resultXml, tc.expectedXml)
}
}
}