2022-02-26 19:38:52 +00:00
package main
import (
"net/http"
"net/http/httptest"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_micropubQuery ( t * testing . T ) {
app := & goBlog {
cfg : createDefaultTestConfig ( t ) ,
}
2022-07-17 05:35:39 +00:00
2022-07-16 19:09:43 +00:00
_ = app . initConfig ( false )
2022-07-17 08:54:03 +00:00
_ = app . initCache ( )
app . initMarkdown ( )
app . initSessions ( )
2022-02-26 19:38:52 +00:00
// Create a test post with tags
err := app . createPost ( & post {
Path : "/test/post" ,
Content : "Test post" ,
Parameters : map [ string ] [ ] string {
"tags" : { "test" , "test2" } ,
} ,
} )
require . NoError ( t , err )
type testCase struct {
query string
want string
wantStatus int
}
testCases := [ ] testCase {
{
query : "config" ,
2022-09-23 11:50:18 +00:00
want : "{\"channels\":[{\"name\":\"default: My Blog\",\"uid\":\"default\"},{\"name\":\"default/posts: posts\",\"uid\":\"default/posts\"}],\"media-endpoint\":\"http://localhost:8080/micropub/media\",\"visibility\":[\"public\",\"unlisted\",\"private\"]}" ,
2022-02-26 19:38:52 +00:00
wantStatus : http . StatusOK ,
} ,
{
query : "source&url=http://localhost:8080/test/post" ,
2022-09-23 09:05:07 +00:00
want : "{\"type\":[\"h-entry\"],\"properties\":{\"published\":[\"\"],\"updated\":[\"\"],\"post-status\":[\"published\"],\"visibility\":[\"public\"],\"category\":[\"test\",\"test2\"],\"content\":[\"---\\nblog: default\\npath: /test/post\\npriority: 0\\npublished: \\\"\\\"\\nsection: \\\"\\\"\\nstatus: published\\ntags:\\n - test\\n - test2\\nupdated: \\\"\\\"\\nvisibility: public\\n---\\nTest post\"],\"url\":[\"http://localhost:8080/test/post\"],\"mp-slug\":[\"\"],\"mp-channel\":[\"default\"]}}" ,
2022-02-26 19:38:52 +00:00
wantStatus : http . StatusOK ,
} ,
{
query : "source" ,
2022-09-23 09:05:07 +00:00
want : "{\"items\":[{\"type\":[\"h-entry\"],\"properties\":{\"published\":[\"\"],\"updated\":[\"\"],\"post-status\":[\"published\"],\"visibility\":[\"public\"],\"category\":[\"test\",\"test2\"],\"content\":[\"---\\nblog: default\\npath: /test/post\\npriority: 0\\npublished: \\\"\\\"\\nsection: \\\"\\\"\\nstatus: published\\ntags:\\n - test\\n - test2\\nupdated: \\\"\\\"\\nvisibility: public\\n---\\nTest post\"],\"url\":[\"http://localhost:8080/test/post\"],\"mp-slug\":[\"\"],\"mp-channel\":[\"default\"]}}]}" ,
2022-02-26 19:38:52 +00:00
wantStatus : http . StatusOK ,
} ,
{
query : "category" ,
2022-04-10 09:46:35 +00:00
want : "{\"categories\":[\"test\",\"test2\"]}" ,
2022-02-26 19:38:52 +00:00
wantStatus : http . StatusOK ,
} ,
2022-09-23 12:15:29 +00:00
{
query : "channel" ,
want : "{\"channels\":[{\"name\":\"default: My Blog\",\"uid\":\"default\"},{\"name\":\"default/posts: posts\",\"uid\":\"default/posts\"}]}" ,
wantStatus : http . StatusOK ,
} ,
2022-02-26 19:38:52 +00:00
{
query : "somethingelse" ,
wantStatus : http . StatusNotFound ,
} ,
}
for _ , tc := range testCases {
req := httptest . NewRequest ( http . MethodGet , "http://localhost:8080/micropub?q=" + tc . query , nil )
rec := httptest . NewRecorder ( )
app . serveMicropubQuery ( rec , req )
rec . Flush ( )
assert . Equal ( t , tc . wantStatus , rec . Code )
if tc . want != "" {
assert . Equal ( t , tc . want , rec . Body . String ( ) )
}
}
}