-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmain_test.go
More file actions
162 lines (133 loc) · 4.13 KB
/
main_test.go
File metadata and controls
162 lines (133 loc) · 4.13 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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//go:build ((tinygo.wasm && wasi) || wasip1) && !nofastlyhostcalls
// Copyright 2022 Fastly, Inc.
package main
import (
"bytes"
"context"
"encoding/json"
"io"
"strconv"
"strings"
"testing"
"github.com/fastly/compute-sdk-go/fsthttp"
"github.com/fastly/compute-sdk-go/fsttest"
"github.com/fastly/compute-sdk-go/internal/abi/fastly"
)
func TestRequestUpstream(t *testing.T) {
t.Run("useAppend=false", func(t *testing.T) { requestUpstream(false, t) })
t.Run("useAppend=true", func(t *testing.T) { requestUpstream(true, t) })
}
func requestUpstream(useAppend bool, t *testing.T) {
handler := func(ctx context.Context, w fsthttp.ResponseWriter, _ *fsthttp.Request) {
// Create our upstream request
req, err := fsthttp.NewRequest("GET", "https://compute-sdk-test-backend.edgecompute.app/request_upstream", nil)
if err != nil {
fsthttp.Error(w, err.Error(), fsthttp.StatusBadGateway)
t.Errorf("NewRequest: %v", err)
return
}
req.Header.Set("UpstreamHeader", "UpstreamValue")
// Make sure the response isn't cached.
req.CacheOptions.Pass = true
// This requires your service to be configured with a backend
// named "TheOrigin" and pointing to "http://provider.org/TheURL".
resp, err := req.Send(ctx, "TheOrigin")
if err != nil {
fsthttp.Error(w, err.Error(), fsthttp.StatusBadGateway)
t.Errorf("Send: %v", err)
return
}
w.Header().Reset(resp.Header.Clone())
w.WriteHeader(resp.StatusCode)
if useAppend {
w.Append(resp.Body)
} else {
io.Copy(w, resp.Body)
}
}
r, err := fsthttp.NewRequest("GET", "/", nil)
if err != nil {
t.Fatalf("NewRequest: %v", err)
}
w := fsttest.NewRecorder()
handler(context.Background(), w, r)
if got, want := w.Code, fsthttp.StatusOK; got != want {
t.Errorf("Code = %d, want %d", got, want)
}
if got, want := w.Header().Get("OriginHeader"), "OriginValue"; got != want {
t.Errorf("Header[OriginHeader] = %q, want %q", got, want)
}
if got, want := w.Header().Get("x-cat"), "meow, nyan, mrrow, miau"; got != want {
t.Errorf("Header[x-cat] = %q, want %q", got, want)
}
if got, want := w.Body.String(), "Hello from Origin"; got != want {
t.Errorf("Body = %q, want %q", got, want)
}
}
const bodySize = 64 * 1024
func TestRequestUpstreamBody(t *testing.T) {
body := make([]byte, bodySize)
for i := range body {
body[i] = 'A'
}
b, err := fastly.NewHTTPBody()
if err != nil {
t.Fatalf("NewHTTPBody: %v", err)
}
_, err = b.Write(body)
if err != nil {
t.Fatalf("Write: %v", err)
}
if err := b.Close(); err != nil {
t.Fatalf("Close: %v", err)
}
testcases := []struct {
name string
body io.Reader
size int
chunked bool
}{
{name: "nil", body: nil},
{name: "bytes.Reader", body: bytes.NewReader(body), size: bodySize},
{name: "bytes.Buffer", body: bytes.NewBuffer(body), size: bodySize},
{name: "strings.Reader", body: strings.NewReader(string(body)), size: bodySize},
{name: "io.NopCloser", body: io.NopCloser(bytes.NewReader(body)), chunked: true},
{name: "fastly.HTTPBody", body: b, chunked: true},
}
for _, tc := range testcases {
t.Run(tc.name, func(t *testing.T) {
requestUpstreamBody(t, tc.body, tc.size, tc.chunked)
})
}
}
func requestUpstreamBody(t *testing.T, body io.Reader, size int, chunked bool) {
req, err := fsthttp.NewRequest("POST", "https://http.edgecompute.app/anything/", body)
if err != nil {
t.Fatalf("NewRequest: %v", err)
}
req.Header.Set("Content-Type", "application/octet-stream")
req.CacheOptions.Pass = true
resp, err := req.Send(context.Background(), "httpme")
if err != nil {
t.Fatalf("Send: %v", err)
}
defer resp.Body.Close()
var respData struct {
Headers map[string]string `json:"headers"`
}
if err := json.NewDecoder(resp.Body).Decode(&respData); err != nil {
t.Fatalf("Decode: %v", err)
}
var teWant, clWant string
if chunked {
teWant = "chunked"
} else {
clWant = strconv.Itoa(size)
}
if got, want := respData.Headers["transfer-encoding"], teWant; got != want {
t.Errorf("Header[transfer-encoding] = %q, want %q", got, want)
}
if got, want := respData.Headers["content-length"], clWant; got != want {
t.Errorf("Header[content-length] = %q, want %q", got, want)
}
}