Add some testing

kcoram/uplift
Jan-Lukas Else 2019-12-24 12:29:14 +01:00
parent 1026a3b2af
commit 35770cecc9
6 changed files with 94 additions and 2 deletions

4
cdn.go
View File

@ -15,9 +15,11 @@ type BunnyCdn struct {
key string
}
var bunnyCdnUrl = "https://bunnycdn.com"
func (cdn *BunnyCdn) Purge(url string) {
client := &http.Client{}
req, _ := http.NewRequest("POST", "https://bunnycdn.com/api/purge?url="+url, nil)
req, _ := http.NewRequest("POST", bunnyCdnUrl+"/api/purge?url="+url, nil)
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
req.Header.Add("AccessKey", cdn.key)

30
cdn_test.go Normal file
View File

@ -0,0 +1,30 @@
package main
import (
"net/http"
"net/http/httptest"
"testing"
)
func TestBunnyCdn_Purge(t *testing.T) {
cdn := &BunnyCdn{
key: "testkey",
}
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if expectedUrl := "/api/purge?url=https://test.url/test"; r.URL.String() != expectedUrl {
t.Errorf("Wrong URL: Got %s, but expected %s", r.URL.String(), expectedUrl)
}
if expectedContentType := "application/json"; r.Header.Get("Content-Type") != expectedContentType {
t.Errorf("Wrong Content-Type Header: Got %s, but expected %s", r.Header.Get("Content-Type"), expectedContentType)
}
if expectedAccept := "application/json"; r.Header.Get("Accept") != expectedAccept {
t.Errorf("Wrong Accept Header: Got %s, but expected %s", r.Header.Get("Accept"), expectedAccept)
}
if r.Header.Get("AccessKey") != cdn.key {
t.Errorf("Wrong AccessKey Header: Got %s, but expected %s", r.Header.Get("AccessKey"), cdn.key)
}
}))
defer ts.Close()
bunnyCdnUrl = ts.URL
cdn.Purge("https://test.url/test")
}

View File

@ -23,7 +23,7 @@ type SyndicationTarget struct {
Name string `json:"name"`
}
func init() {
func initConfig() {
// Blog URL (required)
blogUrl, err := blogUrl()
if err != nil {

View File

@ -8,6 +8,7 @@ import (
)
func main() {
initConfig()
log.Println("Starting micropub server...")
log.Println("Current time: " + time.Now().Format(time.RFC3339))
log.Println("Blog URL: " + BlogUrl)

25
validation_test.go Normal file
View File

@ -0,0 +1,25 @@
package main
import (
"testing"
)
func TestGetContentType(t *testing.T) {
for key, value := range map[string]ContentType{
"": UnsupportedType,
"test": UnsupportedType,
"application/x-www-form-urlencoded": WwwForm,
"application/json": Json,
"multipart/form-data": Multipart,
} {
t.Run(key, func(t *testing.T) {
got, err := GetContentType(key)
if got != value {
t.Errorf("Wrong ContentType")
}
if value == UnsupportedType && err == nil {
t.Errorf("Error is nil")
}
})
}
}

34
webmention_test.go Normal file
View File

@ -0,0 +1,34 @@
package main
import (
"net/http"
"net/http/httptest"
"strconv"
"testing"
)
func Test_responseCodeForSource(t *testing.T) {
for _, code := range []int{200, 404} {
t.Run(strconv.Itoa(code), func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(code)
}))
defer ts.Close()
got, err := responseCodeForSource(ts.URL)
if err != nil || got != code {
t.Errorf("Wrong response code: Got %d, but expected %d", got, code)
}
})
}
t.Run("Error", func(t *testing.T) {
ts := httptest.NewUnstartedServer(nil)
defer ts.Close()
got, err := responseCodeForSource(ts.URL)
if err == nil {
t.Errorf("Error is nil")
}
if got != 0 {
t.Errorf("Wrong response code: Got %d, but expected %d", got, 0)
}
})
}