Compare commits

...

9 Commits

6 changed files with 235 additions and 65 deletions

View File

@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime"
"net/http"
"net/url"
@ -127,7 +126,7 @@ type (
}
Action struct {
Value domain.Action `json:"-"`
domain.Action `json:"-"`
}
bufferHTML struct {
@ -235,7 +234,7 @@ func (h *Handler) handleCreate(w http.ResponseWriter, r *http.Request) {
}
defer file.Close()
content, err := ioutil.ReadAll(file)
content, err := io.ReadAll(file)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
@ -266,17 +265,11 @@ func (h *Handler) handleCreate(w http.ResponseWriter, r *http.Request) {
return
}
w.Header().Set(common.HeaderLocation, out["self"].String())
if len(out)-1 <= 0 {
w.WriteHeader(http.StatusCreated)
return
}
w.Header().Set(common.HeaderLocation, out.URL.String())
links := make([]string, 0)
for rel, value := range out {
links = append(links, `<`+value.String()+`>; rel="`+rel+`"`)
for i := range out.Syndications {
links = append(links, `<`+out.Syndications[i].String()+`>; rel="syndication"`)
}
w.Header().Set(common.HeaderLink, strings.Join(links, ", "))
@ -967,15 +960,15 @@ func (a *Action) UnmarshalJSON(b []byte) error {
return err
}
a.Value = out
a.Action = out
return nil
}
func (a Action) MarshalJSON() ([]byte, error) {
if a.Value == domain.ActionUnd {
if a.Action == domain.ActionUnd {
return []byte(`""`), nil
}
return []byte(strconv.Quote(a.Value.String())), nil
return []byte(strconv.Quote(a.Action.String())), nil
}

View File

@ -2,6 +2,9 @@ package http_test
import (
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"net/url"
"strings"
"testing"
@ -9,19 +12,105 @@ import (
"github.com/google/go-cmp/cmp"
"golang.org/x/net/html"
"source.toby3d.me/toby3d/pub/internal/entry/delivery/http"
"source.toby3d.me/toby3d/pub/internal/common"
"source.toby3d.me/toby3d/pub/internal/domain"
"source.toby3d.me/toby3d/pub/internal/entry"
delivery "source.toby3d.me/toby3d/pub/internal/entry/delivery/http"
"source.toby3d.me/toby3d/pub/internal/media"
)
type testRequest struct {
Delete *http.Delete `json:"delete,omitempty"`
Content []http.Content `json:"content,omitempty"`
Photo []*http.Figure `json:"photo,omitempty"`
Delete *delivery.Delete `json:"delete,omitempty"`
Content []delivery.Content `json:"content,omitempty"`
Photo []*delivery.Figure `json:"photo,omitempty"`
}
func TestHandler_Create(t *testing.T) {
t.Parallel()
t.Run("form", func(t *testing.T) {
t.Parallel()
for name, input := range map[string]url.Values{
"simple": {
"h": []string{"entry"},
"content": []string{"Micropub test of creating a basic h-entry"},
},
"categories": {
"h": []string{"entry"},
"content": []string{"Micropub test of creating an h-entry with categories. " +
"This post should have two categories, test1 and test2"},
"category[]": []string{"test1", "test2"},
},
"category": {
"h": []string{"entry"},
"content": []string{"Micropub test of creating an h-entry with one category. " +
"This post should have one category, test1"},
"category": []string{"test1"},
},
} {
name, input := name, input
t.Run(name, func(t *testing.T) {
t.Parallel()
doCreateRequest(t, strings.NewReader(input.Encode()),
common.MIMEApplicationFormCharsetUTF8)
})
}
})
t.Run("json", func(t *testing.T) {
t.Parallel()
for name, input := range map[string]string{
"simple": `{"type": ["h-entry"], "properties": {"content": ["Micropub test of creating an h-entry with a JSON request"]}}`,
"categories": `{"type": ["h-entry"], "properties": {"content": ["Micropub test of creating an h-entry with a JSON request containing multiple categories. This post should have two categories, test1 and test2."], "category": ["test1", "test2"]}}`,
"html": `{"type": ["h-entry"], "properties": {"content": [{"html": "<p>This post has <b>bold</b> and <i>italic</i> text.</p>"}]}}`,
"photo": `{"type": ["h-entry"], "properties": {"content": ["Micropub test of creating a photo referenced by URL. This post should include a photo of a sunset."], "photo": ["https://micropub.rocks/media/sunset.jpg"]}}`,
"object": `{"type": ["h-entry"], "properties": {"published": ["2017-05-31T12:03:36-07:00"], "content": ["Lunch meeting"], "checkin": [{"type": ["h-card"], "properties": {"name": ["Los Gorditos"], "url": ["https://foursquare.com/v/502c4bbde4b06e61e06d1ebf"], "latitude": [45.524330801154], "longitude": [-122.68068808051], "street-address": ["922 NW Davis St"], "locality": ["Portland"], "region": ["OR"], "country-name": ["United States"], "postal-code": ["97209"]}}]}}`,
"photo-alt": `{"type": ["h-entry"], "properties": {"content": ["Micropub test of creating a photo referenced by URL with alt text. This post should include a photo of a sunset."], "photo": [{"value": "https://micropub.rocks/media/sunset.jpg", "alt": "Photo of a sunset"}]}}`,
"photos": `{"type": ["h-entry"], "properties": {"content": ["Micropub test of creating multiple photos referenced by URL. This post should include a photo of a city at night."], "photo": ["https://micropub.rocks/media/sunset.jpg", "https://micropub.rocks/media/city-at-night.jpg"]}}`,
} {
name, input := name, input
t.Run(name, func(t *testing.T) {
t.Parallel()
doCreateRequest(t, strings.NewReader(input), common.MIMEApplicationJSONCharsetUTF8)
})
}
})
// TODO(toby3d): multipart requests
}
func doCreateRequest(tb testing.TB, r io.Reader, contentType string) {
tb.Helper()
req := httptest.NewRequest(http.MethodPost, "https://example.com/", r)
req.Header.Set(common.HeaderContentType, contentType)
w := httptest.NewRecorder()
delivery.NewHandler(entry.NewStubUseCase(nil, domain.TestEntry(tb), true),
media.NewDummyUseCase()).ServeHTTP(w, req)
resp := w.Result()
if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusAccepted {
tb.Errorf("%s %s = %d, expect %d or %d", req.Method, req.RequestURI, resp.StatusCode,
http.StatusCreated, http.StatusAccepted)
}
if location := resp.Header.Get(common.HeaderLocation); location == "" {
tb.Errorf("%s %s = returns empty Location header, want non-empty", req.Method, req.RequestURI)
}
}
func TestRequest(t *testing.T) {
t.Parallel()
req := new(http.Request)
req := new(delivery.Request)
if err := json.NewDecoder(strings.NewReader(`{
"action": "update",
"url": "http://example.com/",
@ -48,18 +137,18 @@ func TestContent_UnmarshalJSON(t *testing.T) {
for _, tc := range []struct {
name string
in string
out http.Content
out delivery.Content
}{{
name: "plain",
in: `"Hello World"`,
out: http.Content{
out: delivery.Content{
HTML: nil,
Value: "Hello World",
},
}, {
name: "html",
in: `{"html":"<b>Hello</b> <i>World</i>"}`,
out: http.Content{
out: delivery.Content{
HTML: testContent,
Value: "",
},
@ -74,7 +163,7 @@ func TestContent_UnmarshalJSON(t *testing.T) {
t.Fatal(err)
}
if out == nil || len(out.Content) == 0 {
if len(out.Content) == 0 {
t.Error("empty content result, want not nil")
return
@ -96,26 +185,26 @@ func TestContent_MarshalJSON(t *testing.T) {
}
for _, tc := range []struct {
in http.Content
in delivery.Content
out string
name string
}{{
name: "plain",
in: http.Content{
in: delivery.Content{
HTML: nil,
Value: `Hello World`,
},
out: `{"content":["Hello World"]}`,
}, {
name: "html",
in: http.Content{
in: delivery.Content{
HTML: testContent,
Value: "",
},
out: `{"content":[{"html":"\u003cb\u003eHello\u003c/b\u003e \u003ci\u003eWorld\u003c/i\u003e"}]}`,
}, {
name: "both",
in: http.Content{
in: delivery.Content{
HTML: testContent,
Value: `Hello World`,
},
@ -127,7 +216,7 @@ func TestContent_MarshalJSON(t *testing.T) {
t.Parallel()
out, err := json.Marshal(testRequest{
Content: []http.Content{tc.in},
Content: []delivery.Content{tc.in},
})
if err != nil {
t.Fatal(err)
@ -146,22 +235,22 @@ func TestDelete_UnmarshalJSON(t *testing.T) {
for _, tc := range []struct {
name string
in string
out http.Delete
out delivery.Delete
}{{
name: "values",
in: `{"category":["indieweb"]}`,
out: http.Delete{
out: delivery.Delete{
Keys: nil,
Values: http.Properties{
Values: delivery.Properties{
Category: []string{"indieweb"},
},
},
}, {
name: "keys",
in: `["category"]`,
out: http.Delete{
out: delivery.Delete{
Keys: []string{"category"},
Values: http.Properties{},
Values: delivery.Properties{},
},
}} {
tc := tc
@ -187,11 +276,11 @@ func TestFigure_UnmarshalJSON(t *testing.T) {
for _, tc := range []struct {
name string
in string
out http.Figure
out delivery.Figure
}{{
name: "alt",
in: `{"value":"https://photos.example.com/globe.gif","alt":"Spinning globe animation"}`,
out: http.Figure{
out: delivery.Figure{
Alt: "Spinning globe animation",
Value: &url.URL{
Scheme: "https",
@ -202,7 +291,7 @@ func TestFigure_UnmarshalJSON(t *testing.T) {
}, {
name: "plain",
in: `"https://photos.example.com/592829482876343254.jpg"`,
out: http.Figure{
out: delivery.Figure{
Alt: "",
Value: &url.URL{
Scheme: "https",

View File

@ -11,7 +11,7 @@ type (
UseCase interface {
// Create creates a new entry. Returns map or rel links, like Permalink
// or created post, shortcode and syndication.
Create(ctx context.Context, e domain.Entry) (map[string]*url.URL, error)
Create(ctx context.Context, e domain.Entry) (*domain.Entry, error)
// Update updates exist entry properties on provided u.
//
@ -28,27 +28,55 @@ type (
Source(ctx context.Context, u *url.URL) (*domain.Entry, error)
}
stubUseCase struct{}
dummyUseCase struct{}
stubUseCase struct {
entry *domain.Entry
err error
ok bool
}
)
func NewStubUseCase() *stubUseCase {
return &stubUseCase{}
func NewDummyUseCase() *dummyUseCase {
return &dummyUseCase{}
}
func (ucase *stubUseCase) Create(ctx context.Context, e domain.Entry) (map[string]*url.URL, error) {
func (dummyUseCase) Create(ctx context.Context, e domain.Entry) (map[string]*url.URL, error) {
return nil, nil
}
func (dummyUseCase) Update(ctx context.Context, u *url.URL, e domain.Entry) (*domain.Entry, error) {
return nil, nil
}
func (dummyUseCase) Delete(ctx context.Context, u *url.URL) (bool, error) { return false, nil }
func (dummyUseCase) Undelete(ctx context.Context, u *url.URL) (*domain.Entry, error) { return nil, nil }
func (dummyUseCase) Source(ctx context.Context, u *url.URL) (*domain.Entry, error) { return nil, nil }
func NewStubUseCase(err error, e *domain.Entry, ok bool) *stubUseCase {
return &stubUseCase{
entry: e,
err: err,
ok: ok,
}
}
func (ucase *stubUseCase) Create(ctx context.Context, e domain.Entry) (*domain.Entry, error) {
return ucase.entry, ucase.err
}
func (ucase *stubUseCase) Update(ctx context.Context, u *url.URL, e domain.Entry) (*domain.Entry, error) {
return nil, nil
return ucase.entry, ucase.err
}
func (ucase *stubUseCase) Delete(ctx context.Context, u *url.URL) (bool, error) { return false, nil }
func (ucase *stubUseCase) Delete(ctx context.Context, u *url.URL) (bool, error) {
return ucase.ok, ucase.err
}
func (ucase *stubUseCase) Undelete(ctx context.Context, u *url.URL) (*domain.Entry, error) {
return nil, nil
return ucase.entry, ucase.err
}
func (ucase *stubUseCase) Source(ctx context.Context, u *url.URL) (*domain.Entry, error) {
return nil, nil
return ucase.entry, ucase.err
}

View File

@ -1,10 +1,12 @@
package http
import (
"bytes"
"encoding/json"
"io"
"mime"
"net/http"
"time"
"source.toby3d.me/toby3d/pub/internal/common"
"source.toby3d.me/toby3d/pub/internal/domain"
@ -31,21 +33,44 @@ func NewHandler(media media.UseCase, config domain.Config) *Handler {
}
func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
switch r.Method {
default:
WriteError(w, http.StatusText(http.StatusMethodNotAllowed), http.StatusMethodNotAllowed)
case "", http.MethodGet:
h.handleDownload(w, r)
case http.MethodPost:
h.handleUpload(w, r)
}
}
func (h *Handler) handleDownload(w http.ResponseWriter, r *http.Request) {
if r.Method != "" && r.Method != http.MethodGet {
WriteError(w, "method MUST be "+http.MethodGet, http.StatusMethodNotAllowed)
return
}
out, err := h.media.Download(r.Context(), r.RequestURI)
if err != nil {
WriteError(w, "cannot download media: "+err.Error(), http.StatusInternalServerError)
return
}
http.ServeContent(w, r, out.LogicalName(), time.Time{}, bytes.NewReader(out.Content))
}
func (h *Handler) handleUpload(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
WriteError(w, "method MUST be "+http.MethodPost, http.StatusBadRequest)
WriteError(w, "method MUST be "+http.MethodPost, http.StatusMethodNotAllowed)
return
}
mediaType, _, err := mime.ParseMediaType(r.Header.Get(common.HeaderContentType))
if err != nil {
WriteError(w, "Content-Type header MUST be "+common.MIMEMultipartForm, http.StatusBadRequest)
return
}
if mediaType != common.MIMEMultipartForm {
WriteError(w, "Content-Type header MUST be "+common.MIMEMultipartForm, http.StatusBadRequest)
if err != nil || mediaType != common.MIMEMultipartForm {
WriteError(w, common.HeaderContentType+" header MUST be "+common.MIMEMultipartForm,
http.StatusBadRequest)
return
}

View File

@ -2,6 +2,7 @@ package http_test
import (
"bytes"
"io"
"mime/multipart"
"net/http"
"net/http/httptest"
@ -13,7 +14,7 @@ import (
delivery "source.toby3d.me/toby3d/pub/internal/media/delivery/http"
)
func TestUpload(t *testing.T) {
func TestHandler_Upload(t *testing.T) {
t.Parallel()
testConfig := domain.TestConfig(t)
@ -41,7 +42,7 @@ func TestUpload(t *testing.T) {
w := httptest.NewRecorder()
delivery.NewHandler(
media.NewStubUseCase(expect, testFile, nil), *testConfig).
media.NewStubUseCase(nil, testFile, expect), *testConfig).
ServeHTTP(w, req)
resp := w.Result()
@ -54,3 +55,37 @@ func TestUpload(t *testing.T) {
t.Errorf("%s %s = %s, want not empty", req.Method, req.RequestURI, location)
}
}
func TestHandler_Download(t *testing.T) {
t.Parallel()
testConfig := domain.TestConfig(t)
testFile := domain.TestFile(t)
req := httptest.NewRequest(http.MethodGet, "https://example.com/media/"+testFile.LogicalName(), nil)
w := httptest.NewRecorder()
delivery.NewHandler(
media.NewStubUseCase(nil, testFile, nil), *testConfig).
ServeHTTP(w, req)
resp := w.Result()
if resp.StatusCode != http.StatusOK {
t.Errorf("%s %s = %d, want %d", req.Method, req.RequestURI, resp.StatusCode, http.StatusOK)
}
contentType, mediaType := resp.Header.Get(common.HeaderContentType), testFile.MediaType()
if contentType != mediaType {
t.Errorf("%s %s = '%s', want '%s'", req.Method, req.RequestURI, contentType, mediaType)
}
body, err := io.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(body, testFile.Content) {
t.Error("stored and received file contents is not the same")
}
}

View File

@ -20,9 +20,9 @@ type (
dummyUseCase struct{}
stubUseCase struct {
u *url.URL
f *domain.File
err error
u *url.URL
file *domain.File
err error
}
)
@ -35,11 +35,11 @@ func (dummyUseCase) Upload(_ context.Context, _ domain.File) (*url.URL, error)
func (dummyUseCase) Download(_ context.Context, _ string) (*domain.File, error) { return nil, nil }
// NewDummyUseCase creates a stub use case what always returns provided input.
func NewStubUseCase(u *url.URL, f *domain.File, err error) UseCase {
func NewStubUseCase(err error, file *domain.File, u *url.URL) UseCase {
return &stubUseCase{
u: u,
f: f,
err: err,
u: u,
file: file,
err: err,
}
}
@ -48,5 +48,5 @@ func (ucase stubUseCase) Upload(_ context.Context, _ domain.File) (*url.URL, err
}
func (ucase stubUseCase) Download(_ context.Context, _ string) (*domain.File, error) {
return ucase.f, ucase.err
return ucase.file, ucase.err
}