HTTPUseProtoBuf

package module
v0.0.0-...-b0f8e55 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 9, 2020 License: MIT Imports: 3 Imported by: 0

README

HTTP使用protobuf格式通信,客户端测试工具

简介

如果你的web服务器使用json传输数据(或其他明文格式的数据),那就不用往下看了,用postman这个工具测试就好了,很好用。

如果你的web服务器使用protobuf传输数据,那就继续看下去吧。

目录说明

  1. server: web服务器,Go实现。
  2. client: 发送protobuf数据的测试工具,C++实现。
  3. example: 示例,可以直接运行。
  4. proto: protobuf格式的协议。
  5. MockGetIndex: 测试Get请求,没有携带数据。
  6. MockJsonPostLogin: 测试Post请求,发送json格式的数据。
  7. MockProtoPostArr: 测试Post请求, 发送protobuf格式的数据。
  8. MockProtoPostRegister: 测试Post请求, 发送protobuf格式的数据。
  9. MockProtoPostWrap: 测试Post请求, 发送protobuf格式的数据。
  10. MockProtoPostCmplx: 测试Post请求, 发送protobuf格式的数据。

例子演示

打开目录example

双击web.exe打开web服务器,地址为127.0.0.1:9090。(如果打不开的话,就自己编译,代码在server目录下)

双击client.bat,发送protobuf格式的HTTP数据。

使用介绍

这是一个通过HTTP发送protobuf数据的工具,目前数字类型只支持int32。

需要提供test.json和test.proto两个文件。

client.bat 内容如下

parseproto.exe -i "127.0.0.1" -m 9090 -u "/protoPostCmplx" -t "POST" -p "test.proto" -j "test.json"
$ ./parseproto.exe -h
-i "127.0.0.1", web server ip address.
-m 9090, web server port.
-u "/protoPostWrap", URI.
-t "POST", HTTP type, get, post.
-p "test.proto", proto file path.
-j "test.json", json file path.
-h, help.

test.proto

这个test.proto就是客户端和服务器之间实际使用的通信协议。

syntax = "proto2";
package GoBusinessDemo;

message MsgCmplx {
    optional MsgOuter out = 1;
    required Register reg = 2;
    repeated int32 money = 3;
    optional string tag = 4;
}

message MsgOuter {
    optional MsgInner in = 1;
}

message MsgInner {
    optional string name = 1;
}

message Register {
    optional string username = 1;
    optional string password = 2;
    optional int32 age = 3;
    optional string email = 4;
}

如果你想要构造一个MsgCmplx结构体的数据发送出去,那么只需要在test.json中填上如下内容:

json写好之后,麻烦先复制到这个网站上检查下你写的json格式是不是有错:https://www.bejson.com/

还有,如果在test.json中填写一些test.proto中找不到的字段,程序也会挂掉。

{
    "MsgCmplx": {
        "out": {
            "in": {
                "name": "I'm inner."
            }
        },
        "reg": {
            "username": "huangjian",
            "password": "123",
            "age": 28,
            "email": "1342042894@qq.com"
        },
        "money":[1,2,3,4,5,6,7,8],
        "tag":"This is a tag."
    }
}

test.proto 和 test.json 都填好之后,点击client.bat就可以发送数据出去了。

这个工具的主要作用就是把这个json内容变成protobuf格式的内容,作为HTTP发送出去

测试之前要记得先把web服务器打开。

进入目录 server\web.go, 执行go run web.go开启web服务器。

Web服务器接口分类

  1. HTTP类型接口
  2. Websocket类型接口

Websocket接口是一个长连接,很多时候客户端都是有状态的,很难模拟客户端进行测试,所以这里只考虑HTTP类型的接口测试。

HTTP请求分类

  1. HTTP Get
  2. HTTP Post
  3. Put, Delete, ....

我们最经常使用的也就是Get和Post。

HTTP请求分为两个主要部分,url和数据。

Get请求的数据是和url整合在一起的,作为url的一部分发送到web服务器端的。

Post请求的数据一般都是放在body部分的。

测试的时候要考虑测试的接口是否需要携带数据:

  1. 不需要携带数据
  2. 需要携带数据

如果不需要携带数据,那测试就非常简单,直接用浏览器就可以测试了。

如果需要携带数据,那就比较麻烦了,Get请求的数据是放在url中的,相对比较简单,例如:

http://127.0.0.1:9090/login?username=huangjian&password=123

这里主要讨论下Post携带数据。

数据的分类

  1. 简单的文本
  2. json格式数据
  3. xml格式数据
  4. csv格式数据
  5. protobuf格式数据

这里列出了一共5种常用的数据格式,前面4种都是可以手动编辑的,也就是测试起来非常方便(测试工具:postman)。

而第5种,也就是protobuf格式的数据就比较坑了,是二进制格式的,无法手动编辑,所以就有了这个项目。

json和protobuf格式的对比

json明文传输,protobuf二进制编码传输。就是说同样的数据量,protobuf速度更快。但是一般只有传输的数据很多的时候,才会有区别。如果你只是传输几K的数据的话,几乎没什么区别。当然,如果传输的数据量达到了几百M或几百G的时候,那肯定选protobuf,不过这种时候,我们自己一般也都会做数据压缩。

json测试的时候方便得多,建议直接使用json就好了。

如果是长连接的话,需要不断进行通讯,那就选择protobuf。

Documentation

Overview

Package HTTPUseProtoBuf is a generated protocol buffer package.

It is generated from these files:

web.proto

It has these top-level messages:

MsgCmplx
MsgOuter
MsgInner
Register
Student
MsgArr
Test

Index

Constants

View Source
const Default_Test_Type int32 = 77

Variables

View Source
var FOO_name = map[int32]string{
	17: "X",
}
View Source
var FOO_value = map[string]int32{
	"X": 17,
}

Functions

This section is empty.

Types

type FOO

type FOO int32
const (
	FOO_X FOO = 17
)

func (FOO) Enum

func (x FOO) Enum() *FOO

func (FOO) EnumDescriptor

func (FOO) EnumDescriptor() ([]byte, []int)

func (FOO) String

func (x FOO) String() string

func (*FOO) UnmarshalJSON

func (x *FOO) UnmarshalJSON(data []byte) error

type MsgArr

type MsgArr struct {
	Money            []int32 `protobuf:"varint,1,rep,name=money" json:"money,omitempty"`
	XXX_unrecognized []byte  `json:"-"`
}

func (*MsgArr) Descriptor

func (*MsgArr) Descriptor() ([]byte, []int)

func (*MsgArr) GetMoney

func (m *MsgArr) GetMoney() []int32

func (*MsgArr) ProtoMessage

func (*MsgArr) ProtoMessage()

func (*MsgArr) Reset

func (m *MsgArr) Reset()

func (*MsgArr) String

func (m *MsgArr) String() string

type MsgCmplx

type MsgCmplx struct {
	Out              *MsgOuter  `protobuf:"bytes,1,opt,name=out" json:"out,omitempty"`
	Reg              *Register  `protobuf:"bytes,2,req,name=reg" json:"reg,omitempty"`
	Money            []int32    `protobuf:"varint,3,rep,name=money" json:"money,omitempty"`
	Text             []string   `protobuf:"bytes,4,rep,name=text" json:"text,omitempty"`
	Stu              []*Student `protobuf:"bytes,5,rep,name=stu" json:"stu,omitempty"`
	Tag              *string    `protobuf:"bytes,6,opt,name=tag" json:"tag,omitempty"`
	XXX_unrecognized []byte     `json:"-"`
}

func (*MsgCmplx) Descriptor

func (*MsgCmplx) Descriptor() ([]byte, []int)

func (*MsgCmplx) GetMoney

func (m *MsgCmplx) GetMoney() []int32

func (*MsgCmplx) GetOut

func (m *MsgCmplx) GetOut() *MsgOuter

func (*MsgCmplx) GetReg

func (m *MsgCmplx) GetReg() *Register

func (*MsgCmplx) GetStu

func (m *MsgCmplx) GetStu() []*Student

func (*MsgCmplx) GetTag

func (m *MsgCmplx) GetTag() string

func (*MsgCmplx) GetText

func (m *MsgCmplx) GetText() []string

func (*MsgCmplx) ProtoMessage

func (*MsgCmplx) ProtoMessage()

func (*MsgCmplx) Reset

func (m *MsgCmplx) Reset()

func (*MsgCmplx) String

func (m *MsgCmplx) String() string

type MsgInner

type MsgInner struct {
	Name             *string `protobuf:"bytes,1,opt,name=name" json:"name,omitempty"`
	XXX_unrecognized []byte  `json:"-"`
}

func (*MsgInner) Descriptor

func (*MsgInner) Descriptor() ([]byte, []int)

func (*MsgInner) GetName

func (m *MsgInner) GetName() string

func (*MsgInner) ProtoMessage

func (*MsgInner) ProtoMessage()

func (*MsgInner) Reset

func (m *MsgInner) Reset()

func (*MsgInner) String

func (m *MsgInner) String() string

type MsgOuter

type MsgOuter struct {
	In               *MsgInner `protobuf:"bytes,1,opt,name=in" json:"in,omitempty"`
	XXX_unrecognized []byte    `json:"-"`
}

func (*MsgOuter) Descriptor

func (*MsgOuter) Descriptor() ([]byte, []int)

func (*MsgOuter) GetIn

func (m *MsgOuter) GetIn() *MsgInner

func (*MsgOuter) ProtoMessage

func (*MsgOuter) ProtoMessage()

func (*MsgOuter) Reset

func (m *MsgOuter) Reset()

func (*MsgOuter) String

func (m *MsgOuter) String() string

type Register

type Register struct {
	Username         *string `protobuf:"bytes,1,opt,name=username" json:"username,omitempty"`
	Password         *string `protobuf:"bytes,2,opt,name=password" json:"password,omitempty"`
	Age              *int32  `protobuf:"varint,3,opt,name=age" json:"age,omitempty"`
	Email            *string `protobuf:"bytes,4,opt,name=email" json:"email,omitempty"`
	XXX_unrecognized []byte  `json:"-"`
}

func (*Register) Descriptor

func (*Register) Descriptor() ([]byte, []int)

func (*Register) GetAge

func (m *Register) GetAge() int32

func (*Register) GetEmail

func (m *Register) GetEmail() string

func (*Register) GetPassword

func (m *Register) GetPassword() string

func (*Register) GetUsername

func (m *Register) GetUsername() string

func (*Register) ProtoMessage

func (*Register) ProtoMessage()

func (*Register) Reset

func (m *Register) Reset()

func (*Register) String

func (m *Register) String() string

type Student

type Student struct {
	Id               *int32  `protobuf:"varint,1,req,name=id" json:"id,omitempty"`
	Username         *string `protobuf:"bytes,2,req,name=username" json:"username,omitempty"`
	XXX_unrecognized []byte  `json:"-"`
}

func (*Student) Descriptor

func (*Student) Descriptor() ([]byte, []int)

func (*Student) GetId

func (m *Student) GetId() int32

func (*Student) GetUsername

func (m *Student) GetUsername() string

func (*Student) ProtoMessage

func (*Student) ProtoMessage()

func (*Student) Reset

func (m *Student) Reset()

func (*Student) String

func (m *Student) String() string

type Test

type Test struct {
	Label            *string             `protobuf:"bytes,1,req,name=label" json:"label,omitempty"`
	Type             *int32              `protobuf:"varint,2,opt,name=type,def=77" json:"type,omitempty"`
	Reps             []int64             `protobuf:"varint,3,rep,name=reps" json:"reps,omitempty"`
	Optionalgroup    *Test_OptionalGroup `protobuf:"group,4,opt,name=OptionalGroup,json=optionalgroup" json:"optionalgroup,omitempty"`
	XXX_unrecognized []byte              `json:"-"`
}

func (*Test) Descriptor

func (*Test) Descriptor() ([]byte, []int)

func (*Test) GetLabel

func (m *Test) GetLabel() string

func (*Test) GetOptionalgroup

func (m *Test) GetOptionalgroup() *Test_OptionalGroup

func (*Test) GetReps

func (m *Test) GetReps() []int64

func (*Test) GetType

func (m *Test) GetType() int32

func (*Test) ProtoMessage

func (*Test) ProtoMessage()

func (*Test) Reset

func (m *Test) Reset()

func (*Test) String

func (m *Test) String() string

type Test_OptionalGroup

type Test_OptionalGroup struct {
	RequiredField    *string `protobuf:"bytes,5,req,name=RequiredField" json:"RequiredField,omitempty"`
	XXX_unrecognized []byte  `json:"-"`
}

func (*Test_OptionalGroup) Descriptor

func (*Test_OptionalGroup) Descriptor() ([]byte, []int)

func (*Test_OptionalGroup) GetRequiredField

func (m *Test_OptionalGroup) GetRequiredField() string

func (*Test_OptionalGroup) ProtoMessage

func (*Test_OptionalGroup) ProtoMessage()

func (*Test_OptionalGroup) Reset

func (m *Test_OptionalGroup) Reset()

func (*Test_OptionalGroup) String

func (m *Test_OptionalGroup) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL