54 lines
989 B
Go
54 lines
989 B
Go
package tun2
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/Xe/uuid"
|
|
)
|
|
|
|
func TestNewServerNullConfig(t *testing.T) {
|
|
_, err := NewServer(nil)
|
|
if err == nil {
|
|
t.Fatalf("expected NewServer(nil) to fail, got non-failure")
|
|
}
|
|
}
|
|
|
|
func TestGen502Page(t *testing.T) {
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
defer cancel()
|
|
|
|
req, err := http.NewRequest("GET", "http://cetacean.club", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
substring := uuid.New()
|
|
|
|
req = req.WithContext(ctx)
|
|
req.Header.Add("X-Request-Id", substring)
|
|
req.Host = "cetacean.club"
|
|
|
|
resp := gen502Page(req)
|
|
if resp == nil {
|
|
t.Fatalf("expected response to be non-nil")
|
|
}
|
|
|
|
if resp.Body != nil {
|
|
defer resp.Body.Close()
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
if !strings.Contains(string(data), substring) {
|
|
fmt.Println(string(data))
|
|
t.Fatalf("502 page did not contain needed substring %q", substring)
|
|
}
|
|
}
|
|
}
|