示例#1
0
// Test of gowebmsgpack.MsgpackFormatter.Format().
func TestFormatterFormat(t *testing.T) {

	// Create and regist formatter
	formatter := new(MsgpackFormatter)
	goweb.AddFormatter(formatter)

	// Create context
	r, _ := http.NewRequest("GET", "http://localhsot:8080", nil)
	w := httptest.NewRecorder()
	pathPrams := goweb.ParameterValueMap(make(map[string]string))
	cx := &goweb.Context{r, w, pathPrams, MSGPACK_FORMAT}

	data := []int{1, 2, 3}
	result := struct {
		C string
		S int
		D interface{}
		E []string
	}{
		"",
		http.StatusOK,
		data,
		nil,
	}
	expect, _ := msgpack.Marshal(result)
	cx.RespondWithData(data)
	actual := w.Body.Bytes()
	if !bytes.Equal(expect, actual) {
		t.Errorf("expect %v but actual %v.", expect, actual)
	}
}
示例#2
0
// Test of MsgpackRequestDecoder.Decode().
func TestDecoderDecode(t *testing.T) {

	expect := []int{1, 2, 3}
	body, _ := msgpack.Marshal(expect)

	// Create context
	r, _ := http.NewRequest("GET", "http://localhsot:8080", bytes.NewBuffer(body))
	w := httptest.NewRecorder()
	pathPrams := goweb.ParameterValueMap(make(map[string]string))
	cx := &goweb.Context{r, w, pathPrams, MSGPACK_FORMAT}

	// Create Decoder
	decoder := new(MsgpackRequestDecoder)

	var actual []int
	if err := decoder.Unmarshal(cx, &actual); err != nil {
		t.Error(err)
	}

	if len(expect) != len(actual) {
		t.Errorf("expect length of array is %d but actual is %d.", len(expect), len(actual))
	}

	for i := range expect {
		if expect[i] != actual[i] {
			t.Errorf("expect %dth element is %d but actual is %d", expect[i], actual[i])
			break
		}
	}
}
示例#3
0
// Test of gowebmsgpack.MsgpackFormatter.Match().
func TestFormatterMatch(t *testing.T) {

	// Create and regist formatter
	formatter := new(MsgpackFormatter)
	goweb.AddFormatter(formatter)

	// Create context
	r, _ := http.NewRequest("GET", "http://localhsot:8080", nil)
	w := httptest.NewRecorder()
	pathPrams := goweb.ParameterValueMap(make(map[string]string))
	cx := &goweb.Context{r, w, pathPrams, MSGPACK_FORMAT}

	if !formatter.Match(cx) {
		t.Error("expect formatter.Match returns true but actual is false")
	}
}