forked from essentialkaos/slacker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc_test.go
86 lines (79 loc) · 2.14 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
package slack
import (
"context"
"log"
"net/http"
"net/url"
"sync"
"testing"
)
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)
SLACK_API = "http://" + serverAddr + "/"
values := url.Values{
"token": {validToken},
}
responsePartial := &SlackResponse{}
err := post(context.Background(), "parseResponse", values, responsePartial, false)
if err != nil {
t.Errorf("Unexpected error: %s", err)
}
}
func TestParseResponseNoToken(t *testing.T) {
parseResponseOnce.Do(setParseResponseHandler)
once.Do(startServer)
SLACK_API = "http://" + serverAddr + "/"
values := url.Values{}
responsePartial := &SlackResponse{}
err := post(context.Background(), "parseResponse", values, responsePartial, false)
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
if responsePartial.Ok == true {
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)
SLACK_API = "http://" + serverAddr + "/"
values := url.Values{
"token": {"whatever"},
}
responsePartial := &SlackResponse{}
err := post(context.Background(), "parseResponse", values, responsePartial, false)
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
if responsePartial.Ok == true {
t.Errorf("Unexpected error: %s", err)
} else if responsePartial.Error != "invalid_auth" {
t.Errorf("got %v; want %v", responsePartial.Error, "invalid_auth")
}
}