-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_test.go
84 lines (74 loc) · 2.07 KB
/
api_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
/*
* Copyright (c) 2020-present unTill Pro, Ltd.
*/
package router2
import (
"bytes"
"context"
"io/ioutil"
"net/http"
"os"
"testing"
"github.com/stretchr/testify/require"
ibus "github.com/untillpro/airs-ibus"
ibusnats "github.com/untillpro/airs-ibusnats"
"github.com/untillpro/godif"
"github.com/untillpro/godif/services"
)
func TestCheck(t *testing.T) {
setUpHTTPOnly()
defer tearDown()
bodyReader := bytes.NewReader(nil)
resp, err := http.Post("http://127.0.0.1:8822/api/check", "", bodyReader)
require.Nil(t, err, err)
defer resp.Body.Close()
respBodyBytes, err := ioutil.ReadAll(resp.Body)
require.Nil(t, err)
require.Equal(t, "ok", string(respBodyBytes))
expectOKRespPlainText(t, resp)
}
func TestQueueNames(t *testing.T) {
services.SetVerbose(false)
ibusnats.DeclareEmbeddedNATSServer()
initialArgs = os.Args
os.Args = []string{"appPath"}
Declare(context.Background(), "airs-bp", ibus.DefaultTimeout)
var err error
ctx, err = services.ResolveAndStart()
require.Nil(t, err, err)
defer tearDown()
bodyReader := bytes.NewReader(nil)
resp, err := http.Post("http://127.0.0.1:8822/api", "", bodyReader)
require.Nil(t, err, err)
defer resp.Body.Close()
respBodyBytes, err := ioutil.ReadAll(resp.Body)
require.Nil(t, err)
require.Equal(t, `["airs-bp"]`, string(respBodyBytes))
expectOKRespPlainText(t, resp)
}
func Test404(t *testing.T) {
setUpHTTPOnly()
defer tearDown()
bodyReader := bytes.NewReader(nil)
resp, err := http.Post("http://127.0.0.1:8822/api/wrong", "", bodyReader)
require.Nil(t, err, err)
defer resp.Body.Close()
require.Equal(t, http.StatusNotFound, resp.StatusCode)
}
func setUpHTTPOnly() {
initialArgs = os.Args
os.Args = []string{"appPath"}
godif.ProvideSliceElement(&services.Services, &Service{
RouterParams: RouterParams{
Port: DefaultRouterPort,
WriteTimeout: DefaultRouterWriteTimeout,
ReadTimeout: DefaultRouterReadTimeout,
ConnectionsLimit: DefaultRouterConnectionsLimit,
},
})
var err error
services.SetVerbose(false)
if ctx, err = services.ResolveAndStart(); err != nil {
panic(err)
}
}