func (cl *Client) doRequest(req *ldbserver.TransportRequest) (resp *ldbserver.TransportResponse, err error) { if cl == nil { return nil, errors.New("client.DoRequest: call of nil reference") } var ( w io.Writer r io.Reader ) if cl.network == "http" { w = bytes.NewBuffer(nil) } else { r = cl.conn w = cl.conn } if w != nil { var content_type string switch cl.marshaling { case ldbserver.MarshalingTypeJson: enc := json.NewEncoder(w) err = enc.Encode(req) content_type = "application/json" case ldbserver.MarshalingTypeProtobuf: enc := pio.NewUint32DelimitedWriter(w, binary.LittleEndian) err = enc.WriteMsg(req) content_type = "application/octet-stream" } if err != nil { return } if cl.network == "http" { hresp, err := http.Post("http://"+cl.host, content_type, w.(io.Reader)) if err != nil { return nil, err } defer hresp.Body.Close() r = hresp.Body } resp = &ldbserver.TransportResponse{} switch cl.marshaling { case ldbserver.MarshalingTypeJson: dec := json.NewDecoder(r) err = dec.Decode(resp) case ldbserver.MarshalingTypeProtobuf: dec := pio.NewUint32DelimitedReader(r, binary.LittleEndian, 1024*1024) err = dec.ReadMsg(resp) } } else { return nil, errors.New("client.DoRequest: no connection") } return }
func TestLittleUint32MaxSize(t *testing.T) { buf := newBuffer() writer := io.NewUint32DelimitedWriter(buf, binary.LittleEndian) reader := io.NewUint32DelimitedReader(buf, binary.LittleEndian, 20) if err := iotest(writer, reader); err != goio.ErrShortBuffer { t.Error(err) } else { t.Logf("%s", err) } }
func TestLittleUint32Normal(t *testing.T) { buf := newBuffer() writer := io.NewUint32DelimitedWriter(buf, binary.LittleEndian) reader := io.NewUint32DelimitedReader(buf, binary.LittleEndian, 1024*1024) if err := iotest(writer, reader); err != nil { t.Error(err) } if !buf.closed { t.Fatalf("did not close buffer") } }
func serveCommand(t *testing.T, db DBServer, command TransportRequest_Command, key, value []byte, mt MarshalingType, checkOk bool) *TransportResponse { var ( out = bytes.NewBuffer(nil) in = bytes.NewBuffer(nil) tr = JsonProtobufTransportFactory{mt}.NewTransporter(out, in) ) req := &TransportRequest{ Id: key, Command: command.Enum(), Body: &TransportBody{Data: value}, } SetBodyChecksum(req.Body) switch mt { case MarshalingTypeJson: enc := json.NewEncoder(out) assert.NoError(t, enc.Encode(req), "Json") case MarshalingTypeProtobuf: enc := pio.NewUint32DelimitedWriter(out, binary.LittleEndian) assert.NoError(t, enc.WriteMsg(req), "Protobuf") } if assert.NoError(t, db.serve(tr), "db.serve") { resp := &TransportResponse{} switch mt { case MarshalingTypeJson: dec := json.NewDecoder(in) assert.NoError(t, dec.Decode(resp), "Json") case MarshalingTypeProtobuf: dec := pio.NewUint32DelimitedReader(in, binary.LittleEndian, 1024) assert.NoError(t, dec.ReadMsg(resp), "Protobuf") } if checkOk { assert.Equal(t, resp.Id, key, "Response") assert.Equal(t, resp.GetStatus(), TransportResponse_OK, "Response") assert.True(t, CheckBody(resp.Body), "Check resp body") } return resp } return nil }
func (rw *rwTransporter) GetRequest() (req *TransportRequest, err error) { req = &TransportRequest{} switch rw.mt { case MarshalingTypeJson: dec := json.NewDecoder(rw.req) err = dec.Decode(req) case MarshalingTypeProtobuf: dec := pio.NewUint32DelimitedReader(rw.req, binary.LittleEndian, 1024*1024) err = dec.ReadMsg(req) } if err == nil { if !CheckBody(req.Body) { err = errors.New("bad checksum in request body") } } if err != nil { return nil, err } return }