-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain_test.go
50 lines (43 loc) · 1.47 KB
/
main_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
package main
import (
"io"
"net/http"
"os"
"strings"
"testing"
"foxygo.at/jig/log"
"foxygo.at/jig/serve"
"foxygo.at/jig/serve/httprule"
"github.com/stretchr/testify/require"
)
func TestHTTPRuleServer(t *testing.T) {
c := cmdServe{
Proto: []string{"httpgreet/httpgreet.proto"},
ProtoPath: []string{"proto"},
}
opts, err := c.getServerOptions(log.DiscardLogger)
require.NoError(t, err)
ts := serve.NewUnstartedTestServer(serve.JsonnetEvaluator(), os.DirFS("serve/testdata/httpgreet"), opts...)
ts.SetHTTPHandler(httprule.NewServer(ts.Files, ts.UnknownHandler, log.DiscardLogger, nil))
ts.Start()
defer ts.Stop()
baseURL := "http://" + ts.Addr()
resp, err := http.Get(baseURL + "/api/greet/hello/Dolly")
require.NoError(t, err)
defer resp.Body.Close()
b, err := io.ReadAll(resp.Body)
require.NoError(t, err)
require.JSONEq(t, `{"greeting":"httpgreet: Hello, Dolly"}`, string(b))
resp, err = http.Post(baseURL+"/api/greet/hello", "application/json", strings.NewReader(`{"firstName": "Kitty"}`))
require.NoError(t, err)
defer resp.Body.Close()
b, err = io.ReadAll(resp.Body)
require.NoError(t, err)
require.JSONEq(t, `{"greeting":"Thanks for the post, Kitty"}`, string(b))
resp, err = http.Post(baseURL+"/api/greet/world", "application/json", strings.NewReader(`{}`))
require.NoError(t, err)
defer resp.Body.Close()
b, err = io.ReadAll(resp.Body)
require.NoError(t, err)
require.JSONEq(t, `{"greeting":"Thanks for the post and the path, world"}`, string(b))
}