-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute-descriptor_test.go
57 lines (47 loc) · 1.37 KB
/
route-descriptor_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
package navaros_test
import (
"encoding/json"
"testing"
"github.com/RobertWHurst/navaros"
)
func TestRouteDescriptorMarshalJSON(t *testing.T) {
pattern, err := navaros.NewPattern("/a/b/c")
if err != nil {
t.Errorf("Failed to create pattern: %s", err.Error())
}
r := &navaros.RouteDescriptor{
Method: navaros.Get,
Pattern: pattern,
}
bytes, err := r.MarshalJSON()
if err != nil {
t.Errorf("Failed to marshal route descriptor: %s", err.Error())
}
jsonData := map[string]any{}
err = json.Unmarshal(bytes, &jsonData)
if err != nil {
t.Errorf("Failed to unmarshal route descriptor: %s", err.Error())
}
if len(jsonData) != 2 {
t.Errorf("Expected 2 keys, got %d", len(jsonData))
}
if jsonData["Method"] != "GET" {
t.Errorf("Expected Method to be GET, got %s", jsonData["Method"])
}
if jsonData["Pattern"] != "/a/b/c" {
t.Errorf("Expected Pattern to be /a/b/c, got %s", jsonData["Pattern"])
}
}
func TestRouteDescriptorUnmarshalJSON(t *testing.T) {
jsonData := []byte(`{"Method":"GET","Pattern":"/a/b/c"}`)
r := &navaros.RouteDescriptor{}
if err := r.UnmarshalJSON(jsonData); err != nil {
t.Errorf("Failed to unmarshal route descriptor: %s", err.Error())
}
if r.Method != navaros.Get {
t.Errorf("Expected Method to be GET, got %s", r.Method)
}
if r.Pattern.String() != "/a/b/c" {
t.Errorf("Expected Pattern to be /a/b/c, got %s", r.Pattern)
}
}