-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.go
More file actions
43 lines (37 loc) · 744 Bytes
/
main.go
File metadata and controls
43 lines (37 loc) · 744 Bytes
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
package pier
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"os"
"github.com/nojima/httpie-go"
)
func Main(handler http.Handler) {
peer := &Peer{
Handler: handler,
}
peer.Main()
}
type Peer struct {
Handler http.Handler
BaseContext func(*http.Request) context.Context
}
func (p *Peer) RoundTrip(req *http.Request) (*http.Response, error) {
recorder := httptest.NewRecorder()
if p.BaseContext != nil {
req = req.WithContext(p.BaseContext(req))
}
p.Handler.ServeHTTP(recorder, req)
res := recorder.Result()
return res, nil
}
func (p *Peer) Main() {
options := &httpie.Options{
Transport: p,
}
if err := httpie.Main(options); err != nil {
fmt.Fprintf(os.Stderr, "ERROR: %v\n", err)
os.Exit(1)
}
}