/
restfultest.go
104 lines (93 loc) · 3.18 KB
/
restfultest.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package restfultest
import (
"context"
"fmt"
"net/http"
"reflect"
"strings"
"github.com/emicklei/go-restful"
"github.com/go-chassis/go-chassis/core/handler"
"github.com/go-chassis/go-chassis/core/invocation"
chassisRestful "github.com/go-chassis/go-chassis/server/restful"
"github.com/go-mesh/openlogging"
)
//Container is unit test solution for rest api method
type Container struct {
container *restful.Container
ws *restful.WebService
}
//New create a isolated test container,
// you can register a struct, and it will be registered to a isolated container
func New(schema interface{}, chain *handler.Chain) (*Container, error) {
c := new(Container)
c.container = restful.NewContainer()
c.ws = new(restful.WebService)
routes, err := chassisRestful.GetRouteSpecs(schema)
if err != nil {
panic(err)
}
schemaType := reflect.TypeOf(schema)
schemaValue := reflect.ValueOf(schema)
var schemaName string
tokens := strings.Split(schemaType.String(), ".")
if len(tokens) >= 1 {
schemaName = tokens[len(tokens)-1]
}
for _, route := range routes {
method, exist := schemaType.MethodByName(route.ResourceFuncName)
if !exist {
openlogging.GetLogger().Errorf("router func can not find: %s", route.ResourceFuncName)
return nil, fmt.Errorf("router func can not find: %s", route.ResourceFuncName)
}
handler := func(req *restful.Request, rep *restful.Response) {
inv, err := chassisRestful.HTTPRequest2Invocation(req, schemaName, method.Name)
if err != nil {
openlogging.Error("transfer http request to invocation failed", openlogging.WithTags(openlogging.Tags{
"err": err.Error(),
}))
return
}
if chain != nil {
chain.Next(inv, func(ir *invocation.Response) error {
if ir.Err != nil {
if rep != nil {
rep.WriteHeader(ir.Status)
}
return ir.Err
}
chassisRestful.Invocation2HTTPRequest(inv, req)
return nil
})
}
bs := chassisRestful.NewBaseServer(context.Background())
bs.Req = req
bs.Resp = rep
method.Func.Call([]reflect.Value{schemaValue, reflect.ValueOf(bs)})
}
if err = chassisRestful.Register2GoRestful(route, c.ws, handler); err != nil {
return nil, err
}
}
c.container.Add(c.ws)
return c, nil
}
//ServeHTTP accept native httptest, after process, response writer will write response
func (c *Container) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
c.container.ServeHTTP(resp, req)
}