8000 Code changes from editing. · gitgitcode/code@4eecdc6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 4eecdc6

Browse files
Code changes from editing.
1 parent 0affa29 commit 4eecdc6

File tree

3 files changed

+35
-50
lines changed

3 files changed

+35
-50
lines changed

chapter9/listing03/listing03_test.go

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -40,33 +40,6 @@ func mockServer() *httptest.Server {
4040
return httptest.NewServer(http.HandlerFunc(f))
4141
}
4242

43-
// Item defines the fields associated with the item tag in
44-
// the buoy RSS document.
45-
type Item struct {
46-
XMLName xml.Name `xml:"item"`
47-
Title string `xml:"title"`
48-
Description string `xml:"description"`
49-
Link string `xml:"link"`
50-
}
51-
52-
// Channel defines the fields associated with the channel tag in
53-
// the buoy RSS document.
54-
type Channel struct {
55-
XMLName xml.Name `xml:"channel"`
56-
Title string `xml:"title"`
57-
Description string `xml:"description"`
58-
Link string `xml:"link"`
59-
PubDate string `xml:"pubDate"`
60-
Items []Item `xml:"item"`
61-
}
62-
63-
// Document defines the fields associated with the buoy RSS document.
64-
type Document struct {
65-
XMLName xml.Name `xml:"rss"`
66-
Channel Channel `xml:"channel"`
67-
URI string
68-
}
69-
7043
// TestDownload validates the http Get function can download content
7144
// and the content can be unmarshaled and clean.
7245
func TestDownload(t *testing.T) {
@@ -115,3 +88,30 @@ func TestDownload(t *testing.T) {
11588
}
11689
}
11790
}
91+
92+
// Item defines the fields associated with the item tag in
93+
// the buoy RSS document.
94+
type Item struct {
95+
XMLName xml.Name `xml:"item"`
96+
Title string `xml:"title"`
97+
Description string `xml:"description"`
98+
Link string `xml:"link"`
99+
}
100+
101+
// Channel defines the fields associated with the channel tag in
102+
// the buoy RSS document.
103+
type Channel struct {
104+
XMLName xml.Name `xml:"channel"`
105+
Title string `xml:"title"`
106+
Description string `xml:"description"`
107+
Link string `xml:"link"`
108+
PubDate string `xml:"pubDate"`
109+
Items []Item `xml:"item"`
110+
}
111+
112+
// Document defines the fields associated with the buoy RSS document.
113+
type Document struct {
114+
XMLName xml.Name `xml:"rss"`
115+
Channel Channel `xml:"channel"`
116+
URI string
117+
}

chapter9/listing04/handlers/handlers.go

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ package handlers
33

44
import (
55
"encoding/json"
6-
"fmt"
7-
"log"
86
"net/http"
9-
"strconv"
107
)
118

129
// Routes sets the routes for the web service.
@@ -24,19 +21,7 @@ func SendJSON(rw http.ResponseWriter, r *http.Request) {
2421
Email: "bill@ardanstudios.com",
2522
}
2623

27-
data, err := json.Marshal(&u)
28-
if err != nil {
29-
// We want this error condition to panic so we get a
30-
// stack trace. This should never happen. The http
31-
// package will catch the panic and provide logging
32-
// and return a 500 back to the caller.
33-
log.Panic("Unable to unmarshal response", err)
34-
}
35-
36-
datalen := len(data) + 1 // account for trailing LF
37-
h := rw.Header()
38-
h.Set("Content-Type", "application/json")
39-
h.Set("Content-Length", strconv.Itoa(datalen))
24+
rw.Header().Set("Content-Type", "application/json")
4025
rw.WriteHeader(200)
41-
fmt.Fprintf(rw, "%s\n", data)
26+
json.NewEncoder(rw).Encode(&u)
4227
}

chapter9/listing04/handlers/handlers_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,19 @@ func init() {
2222
func TestSendJSON(t *testing.T) {
2323
t.Log("Given the need to test the SendJSON endpoint.")
2424
{
25-
r, err := http.NewRequest("GET", "/sendjson", nil)
25+
req, err := http.NewRequest("GET", "/sendjson", nil)
2626
if err != nil {
2727
t.Fatal("\tShould be able to create a request.",
2828
ballotX, err)
2929
}
3030
t.Log("\tShould be able to create a request.",
3131
checkMark)
3232

33-
w := httptest.NewRecorder()
34-
http.DefaultServeMux.ServeHTTP(w, r)
33+
rw := httptest.NewRecorder()
34+
http.DefaultServeMux.ServeHTTP(rw, req)
3535

36-
if w.Code != 200 {
37-
t.Fatal("\tShould receive \"200\"", ballotX, w.Code)
36+
if rw.Code != 200 {
37+
t.Fatal("\tShould receive \"200\"", ballotX, rw.Code)
3838
}
3939
t.Log("\tShould receive \"200\"", checkMark)
4040

@@ -43,7 +43,7 @@ func TestSendJSON(t *testing.T) {
4343
Email string
4444
}{}
4545

46-
if err := json.NewDecoder(w.Body).Decode(&u); err != nil {
46+
if err := json.NewDecoder(rw.Body).Decode(&u); err != nil {
4747
t.Fatal("\tShould decode the response.", ballotX)
4848
}
4949
t.Log("\tShould decode the response.", checkMark)

0 commit comments

Comments
 (0)
0