package micropub
import (
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
"hawx.me/code/assert"
)
var lastPublished map[string][]interface{}
func fakePublisher(data map[string][]interface{}) (string, error) {
lastPublished = data
return "http://example.com/blog/p/1", nil
}
func TestPostEntry(t *testing.T) {
assert := assert.New(t)
s := httptest.NewServer(postHandler(fakePublisher))
defer s.Close()
resp, err := http.PostForm(s.URL, url.Values{
"h": {"entry"},
"content": {"This is a test"},
"category[]": {"test", "ignore"},
"mp-something": {"what"},
})
assert.Nil(err)
assert.Equal(http.StatusCreated, resp.StatusCode)
assert.Equal("http://example.com/blog/p/1", resp.Header.Get("Location"))
assert.Equal("entry", lastPublished["h"][0])
assert.Equal("This is a test", lastPublished["content"][0])
assert.Equal("test", lastPublished["category"][0])
assert.Equal("ignore", lastPublished["category"][1])
_, ok := lastPublished["mp-something"]
assert.False(ok)
}
func TestPostEntryJSON(t *testing.T) {
assert := assert.New(t)
s := httptest.NewServer(postHandler(fakePublisher))
defer s.Close()
resp, err := http.Post(s.URL, "application/json", strings.NewReader(`{
"type": ["h-entry"],
"properties": {
"content": ["This is a test"],
"category": ["test", "ignore"],
"mp-something": ["what"]
}
}`))
assert.Nil(err)
assert.Equal(http.StatusCreated, resp.StatusCode)
assert.Equal("http://example.com/blog/p/1", resp.Header.Get("Location"))
assert.Equal("entry", lastPublished["h"][0])
assert.Equal("This is a test", lastPublished["content"][0])
assert.Equal("test", lastPublished["category"][0])
assert.Equal("ignore", lastPublished["category"][1])
_, ok := lastPublished["mp-something"]
assert.False(ok)
}
func TestUpdateEntry(t *testing.T) {
assert := assert.New(t)
s := httptest.NewServer(postHandler(fakePublisher))
defer s.Close()
resp, err := http.Post(s.URL, "application/json", strings.NewReader(`{
"action": "update",
"url": "https://example.com/blog/p/100",
"replace": {
"content": ["hello moon"]
},
"add": {
"syndication": ["http://somewhere.com"]
},
"delete": {
"not-important": ["this"]
}
}`))
assert.Nil(err)
assert.Equal(http.StatusBadRequest, resp.StatusCode)
}