Commit 035ba623 authored by Matt Butcher's avatar Matt Butcher

fix(httputil): add test for decoder

parent dec2f060
......@@ -17,6 +17,7 @@ limitations under the License.
package httputil
import (
"bytes"
"encoding/json"
"errors"
"io/ioutil"
......@@ -72,7 +73,58 @@ func TestTextMarshal(t *testing.T) {
}
}
func TestAcceptEncoder(t *testing.T) {
type encDec struct {
Name string
}
func TestDefaultEncoder(t *testing.T) {
in := &encDec{Name: "Foo"}
var out, out2 encDec
fn := func(w http.ResponseWriter, r *http.Request) {
if err := Decode(w, r, &out); err != nil {
t.Fatalf("Failed to decode data: %s", err)
}
if out.Name != in.Name {
t.Fatalf("Expected %q, got %q", in.Name, out.Name)
}
Encode(w, r, out, http.StatusOK)
}
s := httptest.NewServer(http.HandlerFunc(fn))
defer s.Close()
data, err := json.Marshal(in)
if err != nil {
t.Fatalf("Failed to marshal JSON: %s", err)
}
req, err := http.NewRequest("GET", s.URL, bytes.NewBuffer(data))
if err != nil {
t.Fatal(err)
}
req.Header.Set("content-type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
t.Errorf("Failed request: %s", err)
}
if res.StatusCode != http.StatusOK {
t.Errorf("Expected 200, got %d", res.StatusCode)
}
data, err = ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatal(err)
}
if err := json.Unmarshal(data, &out2); err != nil {
t.Fatal(err)
}
if out2.Name != in.Name {
t.Errorf("Expected final output to have name %q, got %q", in.Name, out2.Name)
}
}
func TestAcceptEncoderEncoder(t *testing.T) {
enc := &AcceptEncoder{
DefaultEncoding: "application/json",
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment