-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngrok_test.go
78 lines (70 loc) · 2.12 KB
/
ngrok_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
package ngrok
import (
"context"
"io"
"net/http"
"net/http/httptest"
"net/url"
"os"
"sync"
"testing"
"github.com/kevinburke/rest"
)
// Use this when initializing a new test - first hit the real ngrok API
// with DEBUG_HTTP_TRAFFIC=true in your environment, save the result in
// responses_test, and then replay that response over and over again.
var envClient = New(BaseURL, os.Getenv("NGROK_API_TOKEN"))
var _ = envClient
// this is all taken from kevinburke/twilio-go, MIT licensed
type Server struct {
s *httptest.Server
// copied from httptest.Server
URL string
// URLs of incoming requests, in order
URLs []*url.URL
mu sync.Mutex
}
func newServer(response []byte, code int) *Server {
serv := &Server{URLs: make([]*url.URL, 0)}
s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
serv.mu.Lock()
serv.URLs = append(serv.URLs, r.URL)
serv.mu.Unlock()
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(code)
if _, err := w.Write(response); err != nil {
panic(err)
}
}))
serv.s = s
serv.URL = s.URL
return serv
}
var badContentType = `
{
"details": null,
"error_code": "ERR_NGROK_210",
"msg": "The content type you specified 'application/x-www-form-urlencoded; blah' is not supported by the API. Please check your API client implementation and see the list of supported content types: https://ngrok.com/docs/ngrok-link#service-api-content-type",
"status_code": 415
}
`
func TestErrorParse(t *testing.T) {
errorServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(415)
io.WriteString(w, badContentType)
}))
defer errorServer.Close()
client := New(errorServer.URL, os.Getenv("NGROK_API_TOKEN"))
_, err := client.Creds.Create(context.Background(), url.Values{})
if err == nil {
t.Fatalf("got nil error, want non-nil")
}
rerr, ok := err.(*rest.Error)
if !ok {
t.Fatalf("want a rest.Error, got %v", err)
}
if rerr.ID != "ERR_NGROK_210" {
t.Errorf("bad ID: got %q want ERR_NGROK_210", rerr.ID)
}
}