82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
|
|
package line
|
||
|
|
|
||
|
|
import (
|
||
|
|
"bytes"
|
||
|
|
"net/http"
|
||
|
|
"net/http/httptest"
|
||
|
|
"strings"
|
||
|
|
"testing"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestWebhookRejectsOversizedBody(t *testing.T) {
|
||
|
|
ch := &LINEChannel{}
|
||
|
|
|
||
|
|
oversized := bytes.Repeat([]byte("A"), maxWebhookBodySize+1)
|
||
|
|
req := httptest.NewRequest(http.MethodPost, "/webhook", bytes.NewReader(oversized))
|
||
|
|
rec := httptest.NewRecorder()
|
||
|
|
|
||
|
|
ch.webhookHandler(rec, req)
|
||
|
|
|
||
|
|
if rec.Code != http.StatusRequestEntityTooLarge {
|
||
|
|
t.Errorf("expected status %d, got %d", http.StatusRequestEntityTooLarge, rec.Code)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestWebhookAcceptsMaxBodySize(t *testing.T) {
|
||
|
|
ch := &LINEChannel{}
|
||
|
|
|
||
|
|
body := bytes.Repeat([]byte("A"), maxWebhookBodySize)
|
||
|
|
req := httptest.NewRequest(http.MethodPost, "/webhook", bytes.NewReader(body))
|
||
|
|
rec := httptest.NewRecorder()
|
||
|
|
|
||
|
|
ch.webhookHandler(rec, req)
|
||
|
|
|
||
|
|
// Missing signature should be rejected, but the body size should not trigger 413.
|
||
|
|
if rec.Code != http.StatusForbidden {
|
||
|
|
t.Errorf("expected status %d, got %d", http.StatusForbidden, rec.Code)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestWebhookRejectsOversizedBodyBeforeSignatureCheck(t *testing.T) {
|
||
|
|
ch := &LINEChannel{}
|
||
|
|
|
||
|
|
oversized := bytes.Repeat([]byte("A"), maxWebhookBodySize+1)
|
||
|
|
req := httptest.NewRequest(http.MethodPost, "/webhook", bytes.NewReader(oversized))
|
||
|
|
req.Header.Set("X-Line-Signature", "invalidsignature")
|
||
|
|
rec := httptest.NewRecorder()
|
||
|
|
|
||
|
|
ch.webhookHandler(rec, req)
|
||
|
|
|
||
|
|
if rec.Code != http.StatusRequestEntityTooLarge {
|
||
|
|
t.Errorf("expected status %d, got %d", http.StatusRequestEntityTooLarge, rec.Code)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestWebhookRejectsNonPostMethod(t *testing.T) {
|
||
|
|
ch := &LINEChannel{}
|
||
|
|
|
||
|
|
req := httptest.NewRequest(http.MethodGet, "/webhook", nil)
|
||
|
|
rec := httptest.NewRecorder()
|
||
|
|
|
||
|
|
ch.webhookHandler(rec, req)
|
||
|
|
|
||
|
|
if rec.Code != http.StatusMethodNotAllowed {
|
||
|
|
t.Errorf("expected status %d, got %d", http.StatusMethodNotAllowed, rec.Code)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestWebhookRejectsInvalidSignature(t *testing.T) {
|
||
|
|
ch := &LINEChannel{}
|
||
|
|
|
||
|
|
body := `{"events":[]}`
|
||
|
|
req := httptest.NewRequest(http.MethodPost, "/webhook", strings.NewReader(body))
|
||
|
|
req.Header.Set("X-Line-Signature", "invalidsignature")
|
||
|
|
rec := httptest.NewRecorder()
|
||
|
|
|
||
|
|
ch.webhookHandler(rec, req)
|
||
|
|
|
||
|
|
if rec.Code != http.StatusForbidden {
|
||
|
|
t.Errorf("expected status %d, got %d", http.StatusForbidden, rec.Code)
|
||
|
|
}
|
||
|
|
}
|