diff --git a/cdn.go b/cdn.go index 5acf9b4..e5698ca 100644 --- a/cdn.go +++ b/cdn.go @@ -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) diff --git a/cdn_test.go b/cdn_test.go new file mode 100644 index 0000000..8255d17 --- /dev/null +++ b/cdn_test.go @@ -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") +} diff --git a/config.go b/config.go index dc47399..4802cce 100644 --- a/config.go +++ b/config.go @@ -23,7 +23,7 @@ type SyndicationTarget struct { Name string `json:"name"` } -func init() { +func initConfig() { // Blog URL (required) blogUrl, err := blogUrl() if err != nil { diff --git a/main.go b/main.go index 1227427..3f935e8 100644 --- a/main.go +++ b/main.go @@ -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) diff --git a/validation_test.go b/validation_test.go new file mode 100644 index 0000000..548292f --- /dev/null +++ b/validation_test.go @@ -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") + } + }) + } +} diff --git a/webmention_test.go b/webmention_test.go new file mode 100644 index 0000000..495225b --- /dev/null +++ b/webmention_test.go @@ -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) + } + }) +}