-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ff36250
commit 84f485c
Showing
9 changed files
with
444 additions
and
251 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package api | ||
|
||
import "testing" | ||
|
||
func TestJoinPatternPath(t *testing.T) { | ||
for _, c := range []struct { | ||
elem []string | ||
want string | ||
}{ | ||
{[]string{}, ""}, | ||
{[]string{""}, ""}, | ||
{[]string{"a"}, "a"}, | ||
{[]string{"/"}, "/"}, | ||
{[]string{"/a"}, "/a"}, | ||
{[]string{"a/"}, "a/"}, | ||
{[]string{"/a/"}, "/a/"}, | ||
{[]string{"", "b"}, "b"}, | ||
{[]string{"", "/b"}, "/b"}, | ||
{[]string{"", "b/"}, "b/"}, | ||
{[]string{"", "/b/"}, "/b/"}, | ||
{[]string{"a", "b"}, "a/b"}, | ||
{[]string{"a", "/b"}, "a/b"}, | ||
{[]string{"a", "b/"}, "a/b/"}, | ||
{[]string{"a", "/b/"}, "a/b/"}, | ||
{[]string{"/", "b"}, "/b"}, | ||
{[]string{"/", "/b"}, "/b"}, | ||
{[]string{"/", "b/"}, "/b/"}, | ||
{[]string{"/", "/b/"}, "/b/"}, | ||
{[]string{"/a", "b"}, "/a/b"}, | ||
{[]string{"/a", "/b"}, "/a/b"}, | ||
{[]string{"/a", "b/"}, "/a/b/"}, | ||
{[]string{"/a", "/b/"}, "/a/b/"}, | ||
{[]string{"a/", "b"}, "a/b"}, | ||
{[]string{"a/", "/b"}, "a/b"}, | ||
{[]string{"a/", "b/"}, "a/b/"}, | ||
{[]string{"a/", "/b/"}, "a/b/"}, | ||
{[]string{"/a/", "b"}, "/a/b"}, | ||
{[]string{"/a/", "/b"}, "/a/b"}, | ||
{[]string{"/a/", "b/"}, "/a/b/"}, | ||
{[]string{"/a/", "/b/"}, "/a/b/"}, | ||
} { | ||
if got := joinPatternPath(c.elem...); got != c.want { | ||
t.Errorf("joinPatternPath(%#v) = %q; want %q", c.elem, got, c.want) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package restapi | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
// HandlerFunc is like [http.HandlerFunc], but returns a status code and an error. | ||
type HandlerFunc func(w http.ResponseWriter, r *http.Request) (status int, err error) | ||
|
||
// EncodeResponse sets the Content-Type header field to application/json, and writes | ||
// to the response writer with the given status code and data encoded as JSON. | ||
// | ||
// If data is nil, the status code is written and no data is encoded. | ||
func EncodeResponse(w http.ResponseWriter, status int, data any) (int, error) { | ||
if data == nil { | ||
w.WriteHeader(status) | ||
return status, nil | ||
} | ||
w.Header()["Content-Type"] = []string{"application/json"} | ||
w.WriteHeader(status) | ||
return status, json.NewEncoder(w).Encode(data) | ||
} | ||
|
||
// DecodeRequest decodes the request body as JSON into the provided value. | ||
func DecodeRequest(r *http.Request, v any) error { | ||
return json.NewDecoder(r.Body).Decode(v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.