-
Notifications
You must be signed in to change notification settings - Fork 0
/
post_cancel_post.go
77 lines (70 loc) · 2.25 KB
/
post_cancel_post.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
package check
import (
"context"
"github.com/google/uuid"
"net/http"
"strings"
"time"
)
func post_cancel_post() Check {
return Check{
Name: getCheckName(),
run: func(config *Config, reporter RunCheckReporter) {
defer reporter.Close()
// TODO: implement for HTTP/1.0
if config.Protocol == ProtocolHttp1_0 || config.Protocol == ProtocolHttp1_0_tls {
reporter.Report(RunCheckResult{Warnings: []ResultWarning{NewWarning("Sorry. This check does not support HTTP/1.0 yet", nil)}})
return
}
serverUrl, ok, stopServerIfNeed := prepareServerUrl(config, reporter)
if !ok {
return
}
defer stopServerIfNeed()
postHttpClient := newHTTPClient(config.Protocol, config.TlsSkipVerifyCert)
defer postHttpClient.CloseIdleConnections()
path := "/" + uuid.NewString()
bodyString := "my message"
url := serverUrl + path
contentType := "text/plain"
{
postReq1, err := http.NewRequest("POST", url, strings.NewReader(bodyString))
if err != nil {
reporter.Report(NewRunCheckResultWithOneError(NewError("failed to create POST request", err)))
return
}
ctx, cancel := context.WithCancel(context.Background())
postReq1 = postReq1.WithContext(ctx)
postReq1.Header.Set("Content-Type", contentType)
postResp1, postOk := sendOrGetAndCheck(postHttpClient, postReq1, config.Protocol, reporter)
if !postOk {
return
}
// .Close() is need for piping-server HTTP/2
if err = postResp1.Body.Close(); err != nil {
reporter.Report(NewRunCheckResultWithOneError(NewError("failed to close POST response body", err)))
return
}
cancel()
}
// Without this, it works in local but not work in GitHub Actions
time.Sleep(config.WaitDurationAfterSenderCancel)
{
postReq2, err := http.NewRequest("POST", url, strings.NewReader(bodyString))
if err != nil {
reporter.Report(NewRunCheckResultWithOneError(NewError("failed to create POST request", err)))
return
}
postReq2.Header.Set("Content-Type", contentType)
_, postOk := sendOrGetAndCheck(postHttpClient, postReq2, config.Protocol, reporter)
if !postOk {
return
}
// TODO: use postResp and transfer data
reporter.Report(RunCheckResult{})
}
// TODO: transfer check
return
},
}
}