Esempio n. 1
0
// WriteRequest writes a http.Request to the given writers.
func WriteRequest(call argWriter, req *http.Request) error {
	// TODO(prashant): Allow creating write buffers that let you grow the buffer underneath.
	wb := typed.NewWriteBufferWithSize(10000)
	wb.WriteLen8String(req.Method)
	writeVarintString(wb, req.URL.String())
	writeHeaders(wb, req.Header)

	arg2Writer, err := call.Arg2Writer()
	if err != nil {
		return err
	}
	if _, err := wb.FlushTo(arg2Writer); err != nil {
		return err
	}
	if err := arg2Writer.Close(); err != nil {
		return err
	}

	arg3Writer, err := call.Arg3Writer()
	if err != nil {
		return err
	}

	if req.Body != nil {
		if _, err = io.Copy(arg3Writer, req.Body); err != nil {
			return err
		}
	}
	return arg3Writer.Close()
}
Esempio n. 2
0
func assertRoundTrip(t *testing.T, expected message, actual message) {
	w := typed.NewWriteBufferWithSize(1024)
	require.Nil(t, expected.write(w), fmt.Sprintf("error writing message %v", expected.messageType()))

	var b bytes.Buffer
	w.FlushTo(&b)

	r := typed.NewReadBufferWithSize(1024)
	_, err := r.FillFrom(bytes.NewReader(b.Bytes()), len(b.Bytes()))
	require.Nil(t, err)
	require.Nil(t, actual.read(r), fmt.Sprintf("error reading message %v", expected.messageType()))

	assert.Equal(t, expected, actual, fmt.Sprintf("pre- and post-marshal %v do not match", expected.messageType()))
}
Esempio n. 3
0
func TestFraming(t *testing.T) {
	fh := fakeHeader()
	wbuf := typed.NewWriteBufferWithSize(1024)
	require.Nil(t, fh.write(wbuf))

	var b bytes.Buffer
	if _, err := wbuf.FlushTo(&b); err != nil {
		require.Nil(t, err)
	}

	rbuf := typed.NewReadBuffer(b.Bytes())

	var fh2 FrameHeader
	require.Nil(t, fh2.read(rbuf))

	assert.Equal(t, fh, fh2)
}
Esempio n. 4
0
// writeHeaders writes out the HTTP headers as arg2, and creates the arg3 writer.
func (w *tchanResponseWriter) writeHeaders() {
	// TODO(prashant): Allow creating write buffers that let you grow the buffer underneath.
	wb := typed.NewWriteBufferWithSize(10000)
	wb.WriteUint16(uint16(w.statusCode))
	writeVarintString(wb, http.StatusText(w.statusCode))
	writeHeaders(wb, w.headers)

	arg2Writer, err := w.response.Arg2Writer()
	if err != nil {
		w.err = err
		return
	}
	if _, w.err = wb.FlushTo(arg2Writer); w.err != nil {
		return
	}
	if w.err = arg2Writer.Close(); w.err != nil {
		return
	}

	w.arg3Writer, w.err = w.response.Arg3Writer()
}
Esempio n. 5
0
func TestFraming(t *testing.T) {
	fh := FrameHeader{
		size:        uint16(0xFF34),
		messageType: messageTypeCallReq,
		ID:          0xDEADBEEF,
	}

	wbuf := typed.NewWriteBufferWithSize(1024)
	require.Nil(t, fh.write(wbuf))

	var b bytes.Buffer
	if _, err := wbuf.FlushTo(&b); err != nil {
		require.Nil(t, err)
	}

	rbuf := typed.NewReadBuffer(b.Bytes())

	var fh2 FrameHeader
	require.Nil(t, fh2.read(rbuf))

	assert.Equal(t, fh, fh2)
}