-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample_json_request_test.go
More file actions
52 lines (44 loc) · 1.08 KB
/
Copy pathexample_json_request_test.go
File metadata and controls
52 lines (44 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package box_test
import (
"fmt"
"io"
"net/http"
"net/http/httptest"
"os"
"strings"
"time"
"github.com/fulldump/box"
)
type CreateArticleRequest struct {
Title string
Text string
}
type Article struct {
Id string `json:"id"`
Title string `json:"title"`
Text string `json:"text"`
Created time.Time `json:"created"`
}
func ExampleB_ServeHTTP() {
b := box.NewBox()
b.Handle("POST", "/articles", func(input CreateArticleRequest) Article {
fmt.Println("Persist new article...", input)
return Article{
Id: "my-new-id",
Title: input.Title,
Text: input.Text,
Created: time.Unix(1674762079, 0).UTC(),
}
})
s := httptest.NewServer(b)
defer s.Close()
// go b.ListenAndServe()
resp, _ := http.Post(s.URL+"/articles", "application/json", strings.NewReader(`{
"title": "My great article",
"text": "Blah blah blah..."
}`))
io.Copy(os.Stdout, resp.Body)
// Output:
// Persist new article... {My great article Blah blah blah...}
// {"id":"my-new-id","title":"My great article","text":"Blah blah blah...","created":"2023-01-26T19:41:19Z"}
}