diff --git a/cmd/Chapter07/Exercise07.03/main_test.go b/cmd/Chapter07/Exercise07.03/main_test.go index 04d3170..faa2410 100644 --- a/cmd/Chapter07/Exercise07.03/main_test.go +++ b/cmd/Chapter07/Exercise07.03/main_test.go @@ -2,6 +2,7 @@ package main import ( "fmt" + "log" "testing" ) @@ -36,7 +37,10 @@ func TestNewRecord(t *testing.T) { t.Run(tc.name, func(t *testing.T) { got := newRecord(tc.inputKey, tc.inputInterface) if got.key != tc.wanted.key { - fmt.Errorf("Got: %v wanted %v", got, tc.wanted.key) + err := fmt.Errorf("Got: %v wanted %v", got, tc.wanted.key) + if err != nil { + log.Fatal(err) + } } }) diff --git a/cmd/Chapter15/Activity15.02/a15.02.go b/cmd/Chapter15/Activity15.02/a15.02.go index cafe5cd..340e27d 100644 --- a/cmd/Chapter15/Activity15.02/a15.02.go +++ b/cmd/Chapter15/Activity15.02/a15.02.go @@ -19,7 +19,11 @@ func (h *PageWithCounter) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(400) return } - w.Write([]byte(bts)) + _, err = w.Write([]byte(bts)) + if err != nil { + log.Fatal(err) + } + } func main() { diff --git a/cmd/Chapter15/Activity15.03/main.go b/cmd/Chapter15/Activity15.03/a15.03.go similarity index 84% rename from cmd/Chapter15/Activity15.03/main.go rename to cmd/Chapter15/Activity15.03/a15.03.go index 6bb8dd6..ffffe47 100644 --- a/cmd/Chapter15/Activity15.03/main.go +++ b/cmd/Chapter15/Activity15.03/a15.03.go @@ -20,17 +20,19 @@ func (h Hello) ServeHTTP(w http.ResponseWriter, r *http.Request) { cust := Visitor{} - name, ok := vl["name"] if ok { cust.Name = strings.Join(name, ",") } - h.tpl.Execute(w, cust) + err := h.tpl.Execute(w, cust) + if err != nil { + log.Fatal(err) + } } // NewHello returns a new Hello handler -func NewHello(tplPath string) (*Hello, error){ +func NewHello(tplPath string) (*Hello, error) { tmpl, err := template.ParseFiles(tplPath) if err != nil { return nil, err @@ -46,4 +48,4 @@ func main() { http.Handle("/", hello) log.Fatal(http.ListenAndServe(":8080", nil)) -} \ No newline at end of file +} diff --git a/cmd/Chapter15/Activity15.03/main_test.go b/cmd/Chapter15/Activity15.03/a15.03_test.go similarity index 62% rename from cmd/Chapter15/Activity15.03/main_test.go rename to cmd/Chapter15/Activity15.03/a15.03_test.go index 88da0ec..bf63ad5 100644 --- a/cmd/Chapter15/Activity15.03/main_test.go +++ b/cmd/Chapter15/Activity15.03/a15.03_test.go @@ -1,10 +1,14 @@ package main + import ( - "io/ioutil" + "io" + "log" "net/http" "net/http/httptest" + "os" "testing" ) + func Test_name(t *testing.T) { hdl, err := NewHello("./index.html") if err != nil { @@ -15,14 +19,17 @@ func Test_name(t *testing.T) { if err != nil { t.Error(err) } - expected, err := ioutil.ReadFile("./teststatics/john.html") + expected, err := os.ReadFile("./teststatics/john.html") if err != nil { t.Error(err) } actual := make([]byte, rsp.ContentLength) - rsp.Body.Read(actual) - if string(actual)!= string(expected) { - t.Errorf("\n%s\n%s", string(expected),string(actual)) + _, err = rsp.Body.Read(actual) + if err != io.EOF { + log.Fatal(err) + } + if string(actual) != string(expected) { + t.Errorf("\n%s\n%s", string(expected), string(actual)) } } @@ -36,13 +43,13 @@ func Test_anonymous(t *testing.T) { if err != nil { t.Error(err) } - expected, err := ioutil.ReadFile("./teststatics/anonymous.html") + expected, err := os.ReadFile("./teststatics/anonymous.html") if err != nil { t.Error(err) } actual := make([]byte, rsp.ContentLength) rsp.Body.Read(actual) - if string(actual)!= string(expected) { - t.Errorf("\n%s\n%s", string(expected),string(actual)) + if string(actual) != string(expected) { + t.Errorf("\n%s\n%s", string(expected), string(actual)) } -} \ No newline at end of file +}