-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathinstance_test.go
More file actions
86 lines (71 loc) · 2.31 KB
/
instance_test.go
File metadata and controls
86 lines (71 loc) · 2.31 KB
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
// Copyright (c) 2019 Timo Savola. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package localhost
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"gate.computer/gate/packet"
"gate.computer/gate/service"
"gate.computer/gate/service/servicetest"
"gate.computer/localhost/internal/flat"
flatbuffers "github.com/google/flatbuffers/go"
"github.com/stretchr/testify/assert"
. "import.name/testing/mustr"
)
const testMaxSendSize = 65536
func TestFactory(t *testing.T) {
f := Must(t, R(New(&Config{Addr: "http://example.invalid"})))
servicetest.FactoryTest(t.Context(), t, f, servicetest.FactorySpec{
NoStreams: true,
AlwaysDiscoverable: true,
})
}
func TestRequest(t *testing.T) {
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, r.Host, "bogus")
assert.Equal(t, r.Method, http.MethodGet)
assert.Equal(t, r.URL.Path, "/test")
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusCreated)
fmt.Fprintln(w, "hellocalhost")
}))
defer s.Close()
u := Must(t, R(url.Parse(s.URL)))
inst := newInstance(&Localhost{u.Scheme, u.Host, s.Client()}, service.InstanceConfig{
Service: packet.Service{
MaxSendSize: testMaxSendSize,
Code: servicetest.Code,
},
})
b := flatbuffers.NewBuilder(0)
method := b.CreateString(http.MethodGet)
uri := b.CreateString("//bogus/test")
flat.RequestStart(b)
flat.RequestAddMethod(b, method)
flat.RequestAddUri(b, uri)
request := flat.RequestEnd(b)
flat.CallStart(b)
flat.CallAddFunctionType(b, flat.FunctionRequest)
flat.CallAddFunction(b, request)
b.Finish(flat.CallEnd(b))
p := append(packet.MakeCall(servicetest.Code, 0), b.FinishedBytes()...)
c := make(chan packet.Thunk, 1)
if err := inst.Start(context.Background(), c, nil); err != nil {
t.Fatal(err)
}
p = Must(t, R(inst.Handle(context.Background(), c, p)))
for len(p) == 0 {
p = Must(t, R((<-c)()))
}
assert.True(t, packet.IsValidCall(p, servicetest.Code))
r := flat.GetRootAsResponse(p, packet.HeaderSize)
assert.Equal(t, int(r.StatusCode()), http.StatusCreated)
assert.Equal(t, string(r.ContentType()), "text/plain")
assert.Equal(t, r.BodyLength(), 13)
assert.Equal(t, r.BodyBytes(), []byte("hellocalhost\n"))
}