-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver_test.go
194 lines (176 loc) · 5.75 KB
/
receiver_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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
package webmention_test
import (
"context"
"errors"
"fmt"
webmention "github.com/cvanloo/gowebmention"
"io"
"log"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"sync"
"testing"
"time"
)
func ExampleReceiver() {
acceptForTargetUrl := must(url.Parse("https://example.com"))
webmentionee := webmention.NewReceiver(
webmention.WithAcceptsFunc(func(source, target *url.URL) bool {
return acceptForTargetUrl.Scheme == target.Scheme && acceptForTargetUrl.Host == target.Host
}),
webmention.WithNotifier(webmention.NotifierFunc(func(mention webmention.Mention) {
fmt.Printf("received webmention from %s for %s, status %s", mention.Source, mention.Target, mention.Status)
})),
)
mux := &http.ServeMux{}
mux.Handle("/api/webmention", webmentionee)
srv := http.Server{
Addr: ":8080",
Handler: mux,
}
// [!] should be started before http handler starts receiving
// [!] You can start as many processing goroutines as you'd like
go webmentionee.ProcessMentions()
go func() {
if err := srv.ListenAndServe(); err != nil && !errors.Is(err, http.ErrServerClosed) {
log.Printf("http server error: %v", err)
}
}()
// [!] Once it's time for shutdown...
shutdownCtx, release := context.WithTimeout(context.Background(), 20*time.Second)
defer release()
srv.SetKeepAlivesEnabled(false)
if err := srv.Shutdown(shutdownCtx); err != nil {
log.Printf("http shutdown error: %v", err)
}
// [!] shut down the receiver only after http endpoint stopped
webmentionee.Shutdown(shutdownCtx)
}
func accepts(source, target *url.URL) bool {
switch target.Path {
default:
return true
case "/target/4":
return false
}
}
type TestCase struct {
Comment string
SourceHandler func(ts **httptest.Server) func(w http.ResponseWriter, r *http.Request)
ExpectedHttpStatus int
ExpectedMentionStatus webmention.Status
ExpectedError error
}
var TestCases = []TestCase{
{
Comment: "source links to target",
SourceHandler: func(ts **httptest.Server) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
body := fmt.Sprintf(`<p>Hello, <a href="%s">Target 1</a>!</p>`, (*ts).URL+"/target/1")
w.Write([]byte(body))
}
},
ExpectedHttpStatus: 202,
ExpectedMentionStatus: webmention.StatusLink,
},
{
Comment: "source does not link to target",
SourceHandler: func(**httptest.Server) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("<p>I'm not linking to anything. ;-(</p>"))
}
},
ExpectedHttpStatus: 202, // this type of validation happens async
ExpectedMentionStatus: webmention.StatusNoLink,
},
{
Comment: "source was deleted",
SourceHandler: func(**httptest.Server) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusGone)
w.Write([]byte(`<p>This post was deleted</p>`))
}
},
ExpectedHttpStatus: 202,
ExpectedMentionStatus: webmention.StatusDeleted,
},
{
Comment: "target does not exist or accept webmentions",
SourceHandler: func(ts **httptest.Server) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
body := fmt.Sprintf(`<p>Hello, <a href="%s">Target 4</a>!</p>`, (*ts).URL+"/target/4")
w.Write([]byte(body))
}
},
ExpectedHttpStatus: 400,
//ExpectedMentionStatus
},
{
Comment: "source does not exist",
SourceHandler: func(**httptest.Server) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
w.Write([]byte("Resource not found"))
}
},
ExpectedHttpStatus: 202,
//ExpectedMentionStatus
ExpectedError: webmention.ErrSourceNotFound,
},
}
func TestReceiveLocal(t *testing.T) {
var ts *httptest.Server
wg := sync.WaitGroup{}
wg.Add(len(TestCases)) // either Done() in Report or in NotifierFunc, or in error cases
webmention.Report = func(err error, mention webmention.Mention) {
if err != nil {
defer wg.Done()
testNumber := must(strconv.Atoi(string(mention.Source.Path[len("/source/"):])))
testCase := TestCases[testNumber-1]
if testCase.ExpectedError == nil || !errors.Is(err, testCase.ExpectedError) {
t.Errorf("incorrect error: got: %s, want: %s", err, testCase.ExpectedError)
}
}
}
receiver := webmention.NewReceiver(
webmention.WithAcceptsFunc(accepts),
webmention.WithNotifier(webmention.NotifierFunc(func(mention webmention.Mention) {
defer wg.Done()
testNumber := must(strconv.Atoi(string(mention.Source.Path[len("/source/"):])))
testCase := TestCases[testNumber-1]
if mention.Status != testCase.ExpectedMentionStatus {
t.Errorf("incorrect status, got: %s, want: %s", mention.Status, testCase.ExpectedMentionStatus)
}
})),
)
go receiver.ProcessMentions()
mux := http.NewServeMux()
mux.Handle("/webmention", receiver)
for i, testCase := range TestCases {
mux.HandleFunc(fmt.Sprintf("/source/%d", i+1), testCase.SourceHandler(&ts))
}
ts = httptest.NewServer(mux)
for i, testCase := range TestCases {
t.Logf("test case: %d: %s", i+1, testCase.Comment)
resp, err := http.DefaultClient.PostForm(ts.URL+"/webmention", map[string][]string{
"source": {fmt.Sprintf("%s/source/%d", ts.URL, i+1)},
"target": {fmt.Sprintf("%s/target/%d", ts.URL, i+1)},
})
if err != nil {
wg.Done()
t.Error(err)
continue
}
if resp.StatusCode != testCase.ExpectedHttpStatus {
wg.Done()
t.Errorf("incorrect status code, got: %d, want: %d", resp.StatusCode, testCase.ExpectedHttpStatus)
t.Logf("body: %s", must(io.ReadAll(resp.Body)))
}
if testCase.ExpectedHttpStatus != http.StatusAccepted {
wg.Done() // because listener or report wont ever be invoked
}
}
wg.Wait()
}