mirror of
https://github.com/golang/go
synced 2024-11-23 15:40:06 -07:00
encoding/json: test style tweaks
Rename test name from Http to HTTP, and fix some style nits. Change-Id: I00fe1cecd69ca2f50be86a76ec90031c2f921707 Reviewed-on: https://go-review.googlesource.com/12760 Reviewed-by: Andrew Gerrand <adg@golang.org>
This commit is contained in:
parent
deaf0333df
commit
d0729a6ede
@ -319,43 +319,36 @@ func TestDecodeInStream(t *testing.T) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const raw = `{ "foo": "bar" }`
|
// Test from golang.org/issue/11893
|
||||||
|
func TestHTTPDecoding(t *testing.T) {
|
||||||
|
const raw = `{ "foo": "bar" }`
|
||||||
|
|
||||||
func makeHTTP() io.ReadCloser {
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
mux := http.NewServeMux()
|
|
||||||
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.Write([]byte(raw))
|
w.Write([]byte(raw))
|
||||||
})
|
}))
|
||||||
ts := httptest.NewServer(mux)
|
|
||||||
defer ts.Close()
|
defer ts.Close()
|
||||||
res, err := http.Get(ts.URL)
|
res, err := http.Get(ts.URL)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("GET failed: %v", err)
|
log.Fatalf("GET failed: %v", err)
|
||||||
}
|
}
|
||||||
return res.Body
|
defer res.Body.Close()
|
||||||
}
|
|
||||||
|
|
||||||
func TestHttpDecoding(t *testing.T) {
|
|
||||||
|
|
||||||
foo := struct {
|
foo := struct {
|
||||||
Foo string
|
Foo string
|
||||||
}{}
|
}{}
|
||||||
|
|
||||||
rc := makeHTTP()
|
d := NewDecoder(res.Body)
|
||||||
defer rc.Close()
|
err = d.Decode(&foo)
|
||||||
|
|
||||||
d := NewDecoder(rc)
|
|
||||||
err := d.Decode(&foo)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("Unexpected error %v", err)
|
t.Fatalf("Decode: %v", err)
|
||||||
}
|
}
|
||||||
if foo.Foo != "bar" {
|
if foo.Foo != "bar" {
|
||||||
t.Errorf("Expected \"bar\", was %v", foo.Foo)
|
t.Errorf("decoded %q; want \"bar\"", foo.Foo)
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure we get the EOF the second time
|
// make sure we get the EOF the second time
|
||||||
err = d.Decode(&foo)
|
err = d.Decode(&foo)
|
||||||
if err != io.EOF {
|
if err != io.EOF {
|
||||||
t.Errorf("Expected io.EOF, was %v", err)
|
t.Errorf("err = %v; want io.EOF", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user