Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
projectdiscovery
GitHub Repository: projectdiscovery/nuclei
Path: blob/dev/pkg/fuzz/dataformat/dataformat_test.go
2070 views
1
package dataformat
2
3
import (
4
"testing"
5
)
6
7
func TestDataformatDecodeEncode_JSON(t *testing.T) {
8
obj := `{"foo":"bar"}`
9
10
decoded, err := Decode(obj)
11
if err != nil {
12
t.Fatal(err)
13
}
14
if decoded.DataFormat != "json" {
15
t.Fatal("unexpected data format")
16
}
17
if decoded.Data.Get("foo") != "bar" {
18
t.Fatal("unexpected data")
19
}
20
21
encoded, err := Encode(decoded.Data, decoded.DataFormat)
22
if err != nil {
23
t.Fatal(err)
24
}
25
if encoded != obj {
26
t.Fatal("unexpected data")
27
}
28
}
29
30
func TestDataformatDecodeEncode_XML(t *testing.T) {
31
obj := `<foo attr="baz">bar</foo>`
32
33
decoded, err := Decode(obj)
34
if err != nil {
35
t.Fatal(err)
36
}
37
if decoded.DataFormat != "xml" {
38
t.Fatal("unexpected data format")
39
}
40
fooValue := decoded.Data.Get("foo")
41
if fooValue == nil {
42
t.Fatal("key 'foo' not found")
43
}
44
fooMap, ok := fooValue.(map[string]interface{})
45
if !ok {
46
t.Fatal("type assertion to map[string]interface{} failed")
47
}
48
if fooMap["#text"] != "bar" {
49
t.Fatal("unexpected data for '#text'")
50
}
51
if fooMap["-attr"] != "baz" {
52
t.Fatal("unexpected data for '-attr'")
53
}
54
55
encoded, err := Encode(decoded.Data, decoded.DataFormat)
56
if err != nil {
57
t.Fatal(err)
58
}
59
if encoded != obj {
60
t.Fatal("unexpected data")
61
}
62
}
63
64