Add some testing
parent
1026a3b2af
commit
35770cecc9
4
cdn.go
4
cdn.go
|
@ -15,9 +15,11 @@ type BunnyCdn struct {
|
||||||
key string
|
key string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var bunnyCdnUrl = "https://bunnycdn.com"
|
||||||
|
|
||||||
func (cdn *BunnyCdn) Purge(url string) {
|
func (cdn *BunnyCdn) Purge(url string) {
|
||||||
client := &http.Client{}
|
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("Content-Type", "application/json")
|
||||||
req.Header.Add("Accept", "application/json")
|
req.Header.Add("Accept", "application/json")
|
||||||
req.Header.Add("AccessKey", cdn.key)
|
req.Header.Add("AccessKey", cdn.key)
|
||||||
|
|
|
@ -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")
|
||||||
|
}
|
|
@ -23,7 +23,7 @@ type SyndicationTarget struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func initConfig() {
|
||||||
// Blog URL (required)
|
// Blog URL (required)
|
||||||
blogUrl, err := blogUrl()
|
blogUrl, err := blogUrl()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
1
main.go
1
main.go
|
@ -8,6 +8,7 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
initConfig()
|
||||||
log.Println("Starting micropub server...")
|
log.Println("Starting micropub server...")
|
||||||
log.Println("Current time: " + time.Now().Format(time.RFC3339))
|
log.Println("Current time: " + time.Now().Format(time.RFC3339))
|
||||||
log.Println("Blog URL: " + BlogUrl)
|
log.Println("Blog URL: " + BlogUrl)
|
||||||
|
|
|
@ -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")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
|
@ -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)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
Loading…
Reference in New Issue