forked from qaz74107410/watchtower-line-notifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
84 lines (71 loc) · 2.26 KB
/
main_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
package main
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
"github.com/spf13/viper"
)
func TestWebhookHandler(t *testing.T) {
// Set up a mock LINE Notify server
mockLineServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("Expected POST request, got %s", r.Method)
}
if r.Header.Get("Authorization") != "Bearer testtoken" {
t.Errorf("Expected Authorization header with token, got %s", r.Header.Get("Authorization"))
}
w.WriteHeader(http.StatusOK)
}))
defer mockLineServer.Close()
// Set up Viper configuration
viper.Set("line.api_endpoint", mockLineServer.URL)
viper.Set("line.token", "testtoken")
// Create LineNotifier instance
notifier := &LineNotifier{
APIEndpoint: viper.GetString("line.api_endpoint"),
Token: viper.GetString("line.token"),
}
// Create a test server with our webhookHandler
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
webhookHandler(w, r, notifier)
}))
defer ts.Close()
// Test case
payload := WatchtowerPayload{Text: "Test update"}
jsonPayload, _ := json.Marshal(payload)
resp, err := http.Post(ts.URL+"/webhook", "application/json", bytes.NewBuffer(jsonPayload))
if err != nil {
t.Fatalf("Failed to send POST request: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
t.Errorf("Expected status OK, got %v", resp.Status)
}
}
func TestSendLineNotification(t *testing.T) {
// Set up a mock LINE Notify server
mockLineServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
t.Errorf("Expected POST request, got %s", r.Method)
}
if r.Header.Get("Authorization") != "Bearer testtoken" {
t.Errorf("Expected Authorization header with token, got %s", r.Header.Get("Authorization"))
}
w.WriteHeader(http.StatusOK)
}))
defer mockLineServer.Close()
notifier := &LineNotifier{
APIEndpoint: mockLineServer.URL,
Token: "testtoken",
}
err := notifier.SendLineNotification("Test message")
if err != nil {
t.Errorf("SendLineNotification failed: %v", err)
}
}
func TestGithubAction(t *testing.T) {
// Test githubAction function
_ = "test commit"
}