/
http_mock.go
102 lines (87 loc) · 2.38 KB
/
http_mock.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
/*
Copyright 2021 The CloudEvents Authors
SPDX-License-Identifier: Apache-2.0
*/
package main
import (
"bytes"
"io"
nethttp "net/http"
"net/http/httptest"
"net/url"
cloudevents "github.com/cloudevents/sdk-go/v2"
bindings "github.com/cloudevents/sdk-go/v2/protocol"
"github.com/cloudevents/sdk-go/v2/protocol/http"
)
type RoundTripFunc func(req *nethttp.Request) *nethttp.Response
func (f RoundTripFunc) RoundTrip(req *nethttp.Request) (*nethttp.Response, error) {
return f(req), nil
}
func NewTestClient(fn RoundTripFunc) *nethttp.Client {
return &nethttp.Client{
Transport: RoundTripFunc(fn),
}
}
func MockedSender(options ...http.Option) bindings.Sender {
u, _ := url.Parse("http://localhost")
p, err := http.New(options...)
if err != nil {
panic(err)
}
p.Client = NewTestClient(func(req *nethttp.Request) *nethttp.Response {
return &nethttp.Response{
StatusCode: 202,
Header: make(nethttp.Header),
}
})
p.Target = u
return p
}
func MockedClient() (cloudevents.Client, *http.Protocol) {
mockTransport := RoundTripFunc(func(req *nethttp.Request) *nethttp.Response {
return &nethttp.Response{
StatusCode: 202,
Header: make(nethttp.Header),
Body: io.NopCloser(bytes.NewReader([]byte{})),
}
})
p, err := http.New(http.WithTarget("http://localhost"), http.WithRoundTripper(mockTransport))
if err != nil {
panic(err)
}
client, err := cloudevents.NewClient(p)
if err != nil {
panic(err)
}
return client, p
}
func MockedBinaryRequest(body []byte) *nethttp.Request {
r := httptest.NewRequest("POST", "http://localhost:8080", bytes.NewBuffer(body))
r.Header.Add("Ce-id", "0")
r.Header.Add("Ce-subject", "sub")
r.Header.Add("Ce-specversion", "1.0")
r.Header.Add("Ce-type", "t")
r.Header.Add("Ce-source", "http://localhost")
r.Header.Add("Content-type", "text/plain")
return r
}
var (
eventBegin = []byte("{" +
"\"id\":\"0\"," +
"\"subject\":\"sub\"," +
"\"specversion\":\"1.0\"," +
"\"type\":\"t\"," +
"\"source\":\"http://localhost\"," +
"\"datacontenttype\":\"text/plain\"," +
"\"data\": \"")
eventEnd = []byte("\"}")
)
func MockedStructuredRequest(body []byte) *nethttp.Request {
r := httptest.NewRequest(
"POST",
"http://localhost:8080",
io.MultiReader(bytes.NewReader(eventBegin), bytes.NewBuffer(body), bytes.NewReader(eventEnd)),
)
r.Header.Add("Content-type", cloudevents.ApplicationCloudEventsJSON)
return r
}