Example #1
0
func TestTransformResponse(t *testing.T) {
	tx := rapi.NewTransformer()

	var fixture = `{"One":"this is the one","Two":"this is the second"}`
	res := &http.Response{
		Header:     make(http.Header),
		StatusCode: http.StatusOK,
	}
	res.Body = ioutil.NopCloser(strings.NewReader(fixture))
	require.NotNil(t, res)

	var structure struct {
		One   string
		Three string
	}
	var expected = `{"One":"this is the one","Three":""}`
	ok := tx.TransformResponse(res, &structure, &structure)
	require.True(t, ok)

	body, err := ioutil.ReadAll(res.Body)
	require.Nil(t, err)
	require.NotEmpty(t, body)

	assert.Equal(t, expected, string(body))
}
Example #2
0
func TestTransformCustomTo(t *testing.T) {
	tx := rapi.NewTransformer()

	out := tx.Transform([]byte(from), &From{}, &CustomTo{})
	require.NotNil(t, out)

	var dst bytes.Buffer
	json.Indent(&dst, out, "", "  ")

	assert.Equal(t, to, dst.String())
}
Example #3
0
func TestTransformSameStruct(t *testing.T) {
	tx := rapi.NewTransformer()

	out := tx.Transform([]byte(same), &Same{}, &Same{})
	require.NotNil(t, out)

	var dst bytes.Buffer
	json.Indent(&dst, out, "", "  ")

	assert.Equal(t, same, dst.String())
}
Example #4
0
func TestTransformRequest(t *testing.T) {
	tx := rapi.NewTransformer()

	var fixture = `{"One":"this is the one","Two":"this is the second"}`
	req, err := http.NewRequest("GET", "/test", strings.NewReader(fixture))
	require.Nil(t, err)
	require.NotNil(t, req)

	var structure struct {
		One   string
		Three string
	}
	var expected = `{"One":"this is the one","Three":""}`
	ok := tx.TransformRequest(req, &structure, &structure)
	require.True(t, ok)

	body, err := ioutil.ReadAll(req.Body)
	require.Nil(t, err)
	require.NotEmpty(t, body)

	assert.Equal(t, expected, string(body))
}