forked from slack-go/slack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc_test.go
106 lines (95 loc) · 2.64 KB
/
misc_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
package slack
import (
"context"
"log"
"net/http"
"net/url"
"sync"
"testing"
"github.com/slack-go/slack/slackutilsx"
)
var (
parseResponseOnce sync.Once
)
func parseResponseHandler(rw http.ResponseWriter, r *http.Request) {
rw.Header().Set("Content-Type", "application/json")
token := r.FormValue("token")
log.Println(token)
if token == "" {
rw.Write([]byte(`{"ok":false,"error":"not_authed"}`))
return
}
if token != validToken {
rw.Write([]byte(`{"ok":false,"error":"invalid_auth"}`))
return
}
response := []byte(`{"ok": true}`)
rw.Write(response)
}
func setParseResponseHandler() {
http.HandleFunc("/parseResponse", parseResponseHandler)
}
func TestParseResponse(t *testing.T) {
parseResponseOnce.Do(setParseResponseHandler)
once.Do(startServer)
APIURL := "http://" + serverAddr + "/"
values := url.Values{
"token": {validToken},
}
responsePartial := &SlackResponse{}
err := postForm(context.Background(), http.DefaultClient, APIURL+"parseResponse", values, responsePartial, discard{})
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
}
func TestParseResponseNoToken(t *testing.T) {
parseResponseOnce.Do(setParseResponseHandler)
once.Do(startServer)
APIURL := "http://" + serverAddr + "/"
values := url.Values{}
responsePartial := &SlackResponse{}
err := postForm(context.Background(), http.DefaultClient, APIURL+"parseResponse", values, responsePartial, discard{})
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
if responsePartial.Ok {
t.Errorf("Unexpected error: %s", err)
} else if responsePartial.Error != "not_authed" {
t.Errorf("got %v; want %v", responsePartial.Error, "not_authed")
}
}
func TestParseResponseInvalidToken(t *testing.T) {
parseResponseOnce.Do(setParseResponseHandler)
once.Do(startServer)
APIURL := "http://" + serverAddr + "/"
values := url.Values{
"token": {"whatever"},
}
responsePartial := &SlackResponse{}
err := postForm(context.Background(), http.DefaultClient, APIURL+"parseResponse", values, responsePartial, discard{})
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
if responsePartial.Ok {
t.Errorf("Unexpected error: %s", err)
} else if responsePartial.Error != "invalid_auth" {
t.Errorf("got %v; want %v", responsePartial.Error, "invalid_auth")
}
}
func TestRetryable(t *testing.T) {
for _, e := range []error{
&RateLimitedError{},
StatusCodeError{Code: http.StatusInternalServerError},
StatusCodeError{Code: http.StatusTooManyRequests},
} {
r, ok := e.(slackutilsx.Retryable)
if !ok {
t.Errorf("expected %#v to implement Retryable", e)
}
if !r.Retryable() {
t.Errorf("expected %#v to be Retryable", e)
}
}
}