forked from viant/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
service_router_test.go
138 lines (118 loc) · 3.4 KB
/
service_router_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
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
package toolbox_test
import (
"fmt"
"github.com/stretchr/testify/assert"
"github.com/viant/toolbox"
"log"
"net/http"
"testing"
"time"
)
type ReverseService struct{}
func (this ReverseService) Reverse(values []int) []int {
var result = make([]int, 0)
for i := len(values) - 1; i >= 0; i-- {
result = append(result, values[i])
}
return result
}
func (this ReverseService) Reverse2(values []int) []int {
var result = make([]int, 0)
for i := len(values) - 1; i >= 0; i-- {
result = append(result, values[i])
}
return result
}
var ReverseInvoker = func(serviceRouting *toolbox.ServiceRouting, request *http.Request, response http.ResponseWriter, uriParameters map[string]interface{}) error {
var function = serviceRouting.Handler.(func(values []int) []int)
idsParam := uriParameters["ids"]
ids := idsParam.([]string)
values := make([]int, 0)
for _, item := range ids {
values = append(values, toolbox.AsInt(item))
}
var result = function(values)
err := toolbox.WriteServiceRoutingResponse(response, request, serviceRouting, result)
if err != nil {
return err
}
return nil
}
func StartServer(port string, t *testing.T) {
service := ReverseService{}
router := toolbox.NewServiceRouter(
toolbox.ServiceRouting{
HTTPMethod: "GET",
URI: "/v1/reverse/{ids}",
Handler: service.Reverse,
Parameters: []string{"ids"},
},
toolbox.ServiceRouting{
HTTPMethod: "POST",
URI: "/v1/reverse/",
Handler: service.Reverse,
Parameters: []string{"ids"},
},
toolbox.ServiceRouting{
HTTPMethod: "DELETE",
URI: "/v1/delete/{ids}",
Handler: service.Reverse,
Parameters: []string{"ids"},
},
toolbox.ServiceRouting{
HTTPMethod: "GET",
URI: "/v1/reverse2/{ids}",
Handler: service.Reverse,
Parameters: []string{"ids"},
HandlerInvoker: ReverseInvoker,
},
)
http.HandleFunc("/v1/", func(writer http.ResponseWriter, reader *http.Request) {
err := router.Route(writer, reader)
assert.Nil(t, err)
})
fmt.Printf("Started test server on port %v\n", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}
func TestServiceRouter(t *testing.T) {
go func() {
StartServer("8082", t)
}()
time.Sleep(2 * time.Second)
var result = make([]int, 0)
{
err := toolbox.RouteToService("get", "http://127.0.0.1:8082/v1/reverse/1,7,3", nil, &result)
if err != nil {
t.Errorf("failed to send get request %v", err)
}
assert.EqualValues(t, []int{3, 7, 1}, result)
}
{
err := toolbox.RouteToService("post", "http://127.0.0.1:8082/v1/reverse/", []int{1, 7, 3}, &result)
if err != nil {
t.Errorf("failed to send get request %v", err)
}
assert.EqualValues(t, []int{3, 7, 1}, result)
}
{
err := toolbox.RouteToService("delete", "http://127.0.0.1:8082/v1/delete/", []int{1, 7, 3}, &result)
if err != nil {
t.Errorf("failed to send delete request %v", err)
}
assert.EqualValues(t, []int{3, 7, 1}, result)
}
{
err := toolbox.RouteToService("delete", "http://127.0.0.1:8082/v1/delete/1,7,3", nil, &result)
if err != nil {
t.Errorf("failed to send delete request %v", err)
}
assert.EqualValues(t, []int{3, 7, 1}, result)
}
{ //Test custom handler invocation without reflection
err := toolbox.RouteToService("get", "http://127.0.0.1:8082/v1/reverse2/1,7,3", nil, &result)
if err != nil {
t.Errorf("failed to send delete request %v", err)
}
assert.EqualValues(t, []int{3, 7, 1}, result)
}
}