示例#1
0
func TestStringLiteralDecode(t *testing.T) {
	// D.2.1.  Literal Header Field with Indexing
	var indexing Indexing = WITH
	var name, value string = "custom-key", "custom-header"
	buf := []byte{
		0x40, 0x0a,
		0x63, 0x75,
		0x73, 0x74,
		0x6f, 0x6d,
		0x2d, 0x6b,
		0x65, 0x79,
		0x0d, 0x63,
		0x75, 0x73,
		0x74, 0x6f,
		0x6d, 0x2d,
		0x68, 0x65,
		0x61, 0x64,
		0x65, 0x72,
	}

	sw := swrap.New(buf)
	decoded := DecodeHeader(&sw)
	frame, ok := decoded.(*StringLiteral)
	assert.Equal(t, ok, true)
	expected := NewStringLiteral(indexing, name, value)

	assert.Equal(t, frame, expected)
}
示例#2
0
func TestSettingsCase(t *testing.T) {
	var c TestCase
	framecase := []byte(`{
    "error": null,
    "wire": "00000C040000000000000100002000000300001388",
    "frame": {
      "length": 12,
      "frame_payload": {
        "settings": [
          [
            1,
            8192
          ],
          [
            3,
            5000
          ]
        ],
        "padding_length": null,
        "padding": null
      },
      "flags": 0,
      "stream_identifier": 0,
      "type": 4
    },
    "draft": 14,
    "description": "noraml rst stream frame"
  }`)

	err := json.Unmarshal(framecase, &c)
	if err != nil {
		t.Fatal(err)
	}

	// trace data
	wire := c.Wire
	flags := Flag(c.Frame.Flags)
	streamId := c.Frame.StreamId

	settings := map[SettingsID]int32{
		1: 8192,
		3: 5000,
	}

	// compare struct
	expected := NewSettingsFrame(flags, streamId, settings)
	actual, err := ReadFrame(hexToBuffer(wire))
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, actual, expected)

	// compare wire
	buf := bytes.NewBuffer(make([]byte, 0))
	expected.Write(buf)
	hexdump := strings.ToUpper(hex.EncodeToString(buf.Bytes()))
	assert.Equal(t, wire, hexdump)
}
示例#3
0
func TestHeadersPriorityCase(t *testing.T) {
	var c TestCase
	var p HeadersPriorityPayload
	framecase := []byte(`{
    "error": null,
    "wire": "000023012800000003108000001409746869732069732064756D6D79546869732069732070616464696E672E",
    "frame": {
      "length": 35,
      "frame_payload": {
        "priority": {
          "stream_dependency": 20,
          "weight": 10,
          "exclusive": true
        },
        "header_block_fragment": "this is dummy",
        "padding_length": 16,
        "padding": "This is padding."
      },
      "flags": 40,
      "stream_identifier": 3,
      "type": 1
    },
    "draft": 14,
    "description": "noraml headers frame including priority"
  }`)

	// ignore error
	json.Unmarshal(framecase, &c)
	json.Unmarshal(c.Frame.Payload, &p)

	// trace data
	wire := c.Wire
	flags := Flag(c.Frame.Flags)
	streamId := c.Frame.StreamId
	hb := []byte(p.HeaderBlockFragment)
	padding := []byte(p.Padding)
	dependencyTree := &DependencyTree{
		Exclusive:        p.Priority.Exclusive,
		StreamDependency: p.Priority.StreamDependency,
		Weight:           p.Priority.Weight,
	}

	// compare struct
	expected := NewHeadersFrame(flags, streamId, dependencyTree, hb, padding)
	actual, err := ReadFrame(hexToBuffer(wire))
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, actual, expected)

	// compare wire
	buf := bytes.NewBuffer(make([]byte, 0))
	expected.Write(buf)
	hexdump := strings.ToUpper(hex.EncodeToString(buf.Bytes()))
	assert.Equal(t, wire, hexdump)
}
示例#4
0
func TestIndexedHeaderDecode(t *testing.T) {
	// D.2.4.  Indexed Header Field
	var index uint32 = 2
	buf := []byte{0x82}

	sw := swrap.New(buf)
	decoded := DecodeHeader(&sw)
	frame, ok := decoded.(*IndexedHeader)
	assert.Equal(t, ok, true)
	assert.Equal(t, frame.Index, index)
}
示例#5
0
文件: type_test.go 项目: Jxck/hpack
func TestNewIndexedLiteral(t *testing.T) {
	var indexing Indexing = WITH
	var index uint32 = 10
	var value string = "var"
	var frame *IndexedLiteral = NewIndexedLiteral(indexing, index, value)

	assert.Equal(t, frame.Indexing, indexing)
	assert.Equal(t, frame.Index, index)
	assert.Equal(t, frame.ValueLength, uint32(len(value)))
	assert.Equal(t, frame.ValueString, value)
}
示例#6
0
文件: type_test.go 项目: Jxck/hpack
func TestNewStringLiteral(t *testing.T) {
	var indexing Indexing = WITH
	var name string = "foo"
	var value string = "var"
	var frame *StringLiteral = NewStringLiteral(indexing, name, value)

	assert.Equal(t, frame.Indexing, indexing)
	assert.Equal(t, frame.NameLength, uint32(len(name)))
	assert.Equal(t, frame.NameString, name)
	assert.Equal(t, frame.ValueLength, uint32(len(value)))
	assert.Equal(t, frame.ValueString, value)
}
示例#7
0
// PUSH_PROMISE Frame
func TestPushPromiseCase(t *testing.T) {
	var c TestCase
	var p PushPromisePayload
	framecase := []byte(`{
    "error": null,
    "wire": "000018050800000009060000000B746869732069732064756D6D79486F77647921",
    "frame": {
      "length": 24,
      "frame_payload": {
        "header_block_fragment": "this is dummy",
        "padding_length": 6,
        "promised_stream_id": 11,
        "padding": "Howdy!"
      },
      "flags": 8,
      "stream_identifier": 9,
      "type": 5
    },
    "draft": 14,
    "description": "noraml push promise frame"
  }`)

	// ignore error
	json.Unmarshal(framecase, &c)
	json.Unmarshal(c.Frame.Payload, &p)

	// trace data
	wire := c.Wire
	flags := Flag(c.Frame.Flags)
	streamId := c.Frame.StreamId
	promisedStreamId := p.PromisedStreamID
	headerBlockFragment := []byte(p.HeaderBlockFragment)
	padding := []byte(p.Padding)

	// compare struct
	expected := NewPushPromiseFrame(flags, streamId, promisedStreamId, headerBlockFragment, padding)
	actual, err := ReadFrame(hexToBuffer(wire))
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, actual, expected)

	// compare wire
	buf := bytes.NewBuffer(make([]byte, 0))
	expected.Write(buf)
	hexdump := strings.ToUpper(hex.EncodeToString(buf.Bytes()))
	assert.Equal(t, wire, hexdump)
}
示例#8
0
func TestDynamicTableSizeLen(t *testing.T) {
	ht := DynamicTable{
		DEFAULT_HEADER_TABLE_SIZE,
		[]*HeaderField{
			NewHeaderField("1234", "1234"),
			NewHeaderField("1234", "1234"),
			NewHeaderField("1234", "1234"),
			NewHeaderField("1234", "1234"),
			NewHeaderField("1234", "1234"),
		},
	}

	var actual uint32 = 200
	assert.Equal(t, ht.Size(), actual)
	assert.Equal(t, ht.Len(), 5)
}
示例#9
0
func TestStringLiteralEncode(t *testing.T) {
	// D.2.1.  Literal Header Field with Indexing
	var indexing Indexing = WITH
	var name, value string = "custom-key", "custom-header"
	buf := []byte{
		0x40, 0x0a,
		0x63, 0x75,
		0x73, 0x74,
		0x6f, 0x6d,
		0x2d, 0x6b,
		0x65, 0x79,
		0x0d, 0x63,
		0x75, 0x73,
		0x74, 0x6f,
		0x6d, 0x2d,
		0x68, 0x65,
		0x61, 0x64,
		0x65, 0x72,
	}

	frame := NewStringLiteral(indexing, name, value)
	actual := frame.Encode()

	assert.Equal(t, actual.Bytes(), buf)
}
示例#10
0
func TestGoAwayCase(t *testing.T) {
	var c TestCase
	var p GoAwayPayload
	framecase := []byte(`{
    "error": null,
    "wire": "0000170700000000000000001E00000009687061636B2069732062726F6B656E",
    "frame": {
      "length": 23,
      "frame_payload": {
        "error_code": 9,
        "additional_debug_data": "hpack is broken",
        "padding_length": null,
        "last_stream_id": 30,
        "padding": null
      },
      "flags": 0,
      "stream_identifier": 0,
      "type": 7
    },
    "draft": 14,
    "description": "normal goaway frame"
  }`)

	json.Unmarshal(framecase, &c)
	json.Unmarshal(c.Frame.Payload, &p)

	// trace data
	wire := c.Wire
	streamId := c.Frame.StreamId
	lastStreamId := p.LastStreamID
	errorCode := p.ErrorCode
	additional := []byte(p.AdditionalDebugData)

	// compare struct
	expected := NewGoAwayFrame(streamId, lastStreamId, errorCode, additional)
	actual, err := ReadFrame(hexToBuffer(wire))
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, actual, expected)

	// compare wire
	buf := bytes.NewBuffer(make([]byte, 0))
	expected.Write(buf)
	hexdump := strings.ToUpper(hex.EncodeToString(buf.Bytes()))
	assert.Equal(t, wire, hexdump)
}
示例#11
0
func TestDataCase(t *testing.T) {
	var c TestCase
	var p DataPayload
	framecase := []byte(`{
    "error": null,
    "wire": "0000140008000000020648656C6C6F2C20776F726C6421486F77647921",
    "frame": {
      "length": 20,
      "frame_payload": {
        "data": "Hello, world!",
        "padding_length": 6,
        "padding": "Howdy!"
      },
      "flags": 8,
      "stream_identifier": 2,
      "type": 0
    },
    "draft": 14,
    "description": "noraml data frame"
  }`)

	// ignore error
	json.Unmarshal(framecase, &c)
	json.Unmarshal(c.Frame.Payload, &p)

	// trace data
	wire := c.Wire
	flags := Flag(c.Frame.Flags)
	streamId := c.Frame.StreamId
	data := []byte(p.Data)
	padding := []byte(p.Padding)

	// compare struct
	expected := NewDataFrame(flags, streamId, data, padding)
	actual, err := ReadFrame(hexToBuffer(wire))
	if err != nil {
		t.Fatal(err)
	}
	_ = actual
	assert.Equal(t, actual, expected)

	// compare wire
	buf := bytes.NewBuffer(make([]byte, 0))
	expected.Write(buf)
	hexdump := strings.ToUpper(hex.EncodeToString(buf.Bytes()))
	assert.Equal(t, wire, hexdump)
}
示例#12
0
文件: type_test.go 项目: Jxck/hpack
func TestNewIndexedHeader(t *testing.T) {
	var index uint32 = 10
	var frame *IndexedHeader
	frame = NewIndexedHeader(index)

	actual, expected := frame.Index, index
	assert.Equal(t, actual, expected)
}
示例#13
0
func TestHeadersCase(t *testing.T) {
	var c TestCase
	var p HeadersPayload
	framecase := []byte(`{
    "error": null,
    "wire": "00000D010000000001746869732069732064756D6D79",
    "frame": {
      "length": 13,
      "frame_payload": {
        "priority": null,
        "header_block_fragment": "this is dummy",
        "padding_length": null,
        "padding": null
      },
      "flags": 0,
      "stream_identifier": 1,
      "type": 1
    },
    "draft": 14,
    "description": "noraml headers frame"
  }`)

	// ignore error
	json.Unmarshal(framecase, &c)
	json.Unmarshal(c.Frame.Payload, &p)

	// trace data
	wire := c.Wire
	flags := Flag(c.Frame.Flags)
	streamId := c.Frame.StreamId
	headerBlockFragment := []byte(p.HeaderBlockFragment)

	// compare struct
	expected := NewHeadersFrame(flags, streamId, nil, headerBlockFragment, nil)
	actual, err := ReadFrame(hexToBuffer(wire))
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, actual, expected)

	// compare wire
	buf := bytes.NewBuffer(make([]byte, 0))
	expected.Write(buf)
	hexdump := strings.ToUpper(hex.EncodeToString(buf.Bytes()))
	assert.Equal(t, wire, hexdump)
}
示例#14
0
func TestDecode(t *testing.T) {
	for _, tc := range testCase {
		expected := tc.str
		code := toHexBytes(tc.hex)
		actual := string(Decode(code))
		assert.Equal(t, actual, expected)
	}
}
示例#15
0
func TestEmit(t *testing.T) {
	hl := NewHeaderList()
	hf1 := NewHeaderField("key1", "value1")
	hf2 := NewHeaderField("key2", "value2")
	hl.Emit(hf1)
	hl.Emit(hf2)
	assert.Equal(t, hl.Len(), 2)
}
示例#16
0
func TestEncodeDecode(t *testing.T) {
	for _, tc := range testCase {
		expected := []byte(tc.str)
		encoded := Encode(expected)
		actual := Decode(encoded)
		assert.Equal(t, actual, expected)
	}
}
示例#17
0
// PING Frame
func TestPingCase(t *testing.T) {
	var c TestCase
	var p PingPayload
	framecase := []byte(`{
    "error": null,
    "wire": "0000080600000000006465616462656566",
    "frame": {
      "length": 8,
      "frame_payload": {
        "opaque_data": "deadbeef",
        "padding_length": null,
        "padding": null
      },
      "flags": 0,
      "stream_identifier": 0,
      "type": 6
    },
    "draft": 14,
    "description": "noraml ping frame"
  }`)

	// ignore error
	json.Unmarshal(framecase, &c)
	json.Unmarshal(c.Frame.Payload, &p)

	// trace data
	wire := c.Wire
	flags := Flag(c.Frame.Flags)
	streamId := c.Frame.StreamId
	opaqueData := []byte(p.OpaqueData)

	// compare struct
	expected := NewPingFrame(flags, streamId, opaqueData)
	actual, err := ReadFrame(hexToBuffer(wire))
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, actual, expected)

	// compare wire
	buf := bytes.NewBuffer(make([]byte, 0))
	expected.Write(buf)
	hexdump := strings.ToUpper(hex.EncodeToString(buf.Bytes()))
	assert.Equal(t, wire, hexdump)
}
示例#18
0
func TestIndexedHeaderEncode(t *testing.T) {
	// D.2.3.  Indexed Header Field
	var index uint32 = 2
	buf := []byte{0x82}

	frame := NewIndexedHeader(index)
	actual := frame.Encode()
	assert.Equal(t, actual.Bytes(), buf)
}
示例#19
0
func TestEncode(t *testing.T) {
	for _, tc := range testCase {
		raw := []byte(tc.str)
		expected := tc.hex
		encoded := Encode(raw)
		actual := toHexString(encoded)
		assert.Equal(t, actual, expected)
	}
}
示例#20
0
func TestRstStreamCase(t *testing.T) {
	var c TestCase
	framecase := []byte(`{
    "error": null,
    "wire": "00000403000000000500000008",
    "frame": {
      "length": 4,
      "frame_payload": {
        "error_code": 8,
        "padding_length": null,
        "padding": null
      },
      "flags": 0,
      "stream_identifier": 5,
      "type": 3
    },
    "draft": 14,
    "description": "noraml rst stream frame"
  }`)

	err := json.Unmarshal(framecase, &c)
	if err != nil {
		t.Fatal(err)
	}

	// trace data
	wire := c.Wire
	streamId := c.Frame.StreamId

	// compare struct
	expected := NewRstStreamFrame(streamId, 8)
	actual, err := ReadFrame(hexToBuffer(wire))
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, actual, expected)

	// compare wire
	buf := bytes.NewBuffer(make([]byte, 0))
	expected.Write(buf)
	hexdump := strings.ToUpper(hex.EncodeToString(buf.Bytes()))

	assert.Equal(t, wire, hexdump)
}
示例#21
0
// WINDOW_UPDATE Frame
func TestWindowUpdate(t *testing.T) {
	var c TestCase
	var p WindowUpdatePayload
	framecase := []byte(`{
    "error": null,
    "wire": "000004080000000032000003E8",
    "frame": {
      "length": 4,
      "frame_payload": {
        "window_size_increment": 1000,
        "padding_length": null,
        "padding": null
      },
      "flags": 0,
      "stream_identifier": 50,
      "type": 8
    },
    "draft": 14,
    "description": "noraml window update frame"
  }`)

	json.Unmarshal(framecase, &c)
	json.Unmarshal(c.Frame.Payload, &p)

	// trace data
	wire := c.Wire
	streamId := c.Frame.StreamId
	incrementSize := p.WindowSizeIncrement

	// compare struct
	expected := NewWindowUpdateFrame(streamId, incrementSize)
	actual, err := ReadFrame(hexToBuffer(wire))
	if err != nil {
		t.Fatal(err)
	}
	assert.Equal(t, actual, expected)

	// compare wire
	buf := bytes.NewBuffer(make([]byte, 0))
	expected.Write(buf)
	hexdump := strings.ToUpper(hex.EncodeToString(buf.Bytes()))
	assert.Equal(t, wire, hexdump)
}
示例#22
0
func TestReadPrefixedInteger(t *testing.T) {
	// 0x1F 0001 1111
	// 0x95 1001 0101
	// 0x0A 0000 1010
	// 0x06 0000 0110
	var prefix uint8 = 5
	buf := swrap.New([]byte{0x1F, 0x95, 0x0A, 0x06})
	expected := []byte{0x1F, 0x95, 0xA}
	actual := ReadPrefixedInteger(&buf, prefix)
	assert.Equal(t, actual.Bytes(), expected)
}
示例#23
0
func TestIndexedLiteralDecode(t *testing.T) {
	// D.2.2.  Literal Header Field without Indexing
	var indexing Indexing = WITHOUT
	var index uint32 = 4
	var value string = "/sample/path"
	buf := []byte{
		0x04, 0x0c,
		0x2f, 0x73,
		0x61, 0x6d,
		0x70, 0x6c,
		0x65, 0x2f,
		0x70, 0x61,
		0x74, 0x68,
	}
	// table size: empty

	sw := swrap.New(buf)
	decoded := DecodeHeader(&sw)
	frame, ok := decoded.(*IndexedLiteral)
	assert.Equal(t, ok, true)
	expected := NewIndexedLiteral(indexing, index, value)
	assert.Equal(t, frame, expected)
}
示例#24
0
// RST_STREAM Frame
func TestRstStreamFrame(t *testing.T) {
	expected := NewRstStreamFrame(PROTOCOL_ERROR, 1)

	buf := bytes.NewBuffer(make([]byte, 0))
	expected.Write(buf)

	fh := new(FrameHeader)
	fh.Read(buf)

	actual := new(RstStreamFrame)
	actual.FrameHeader = fh
	actual.Read(buf)

	assert.Equal(t, actual, expected)
}
示例#25
0
// GOAWAY Frame
func TestGoAwayFrame(t *testing.T) {
	expected := NewGoAwayFrame(101, 100, NO_ERROR, nil)

	buf := bytes.NewBuffer(make([]byte, 0))
	expected.Write(buf)

	fh := new(FrameHeader)
	fh.Read(buf)

	actual := new(GoAwayFrame)
	actual.FrameHeader = fh
	actual.Read(buf)

	assert.Equal(t, actual, expected)
}
示例#26
0
// HEADERS Frame
func TestHeadersFrame(t *testing.T) {
	hb := []byte("test header block")
	expected := NewHeadersFrame(END_STREAM, 2, nil, hb, nil)

	buf := bytes.NewBuffer(make([]byte, 0))
	expected.Write(buf)

	fh := new(FrameHeader)
	fh.Read(buf)

	actual := new(HeadersFrame)
	actual.FrameHeader = fh
	actual.Read(buf)

	assert.Equal(t, actual, expected)
}
示例#27
0
func TestEncodeDecode(t *testing.T) {
	context := NewContext(DEFAULT_HEADER_TABLE_SIZE)

	hl := HeaderList{
		NewHeaderField(":status", "200"),
		NewHeaderField("cache-control", "private"),
		NewHeaderField("date", "Mon, 21 Oct 2013 20:13:22 GMT"),
		NewHeaderField("location", "https://www.example.com"),
		NewHeaderField("content-encoding", "gzip"),
		NewHeaderField("set-cookie", "foo=ASDJKHQKBZXOQWEOPIUAXQWEOIU; max-age=3600; version=1"),
	}

	encoded := context.Encode(hl)
	context.Decode(encoded)
	assert.Equal(t, hl, *context.ES)
}
示例#28
0
func TestDecode(t *testing.T) {
	testcases := []struct {
		expected, actual uint32
	}{
		{Decode(swrap.New([]byte{10}), 5), 10},
		{Decode(swrap.New([]byte{31, 9}), 5), 40},
		{Decode(swrap.New([]byte{31, 154, 10}), 5), 1337},
		{Decode(swrap.New([]byte{31, 161, 141, 183, 1}), 5), 3000000},
	}

	for _, testcase := range testcases {
		actual := testcase.actual
		expected := testcase.expected
		assert.Equal(t, actual, expected)
	}
}
示例#29
0
func TestEncode(t *testing.T) {
	testcases := []struct {
		actual   swrap.SWrap
		expected []byte
	}{
		{Encode(10, 5), []byte{10}},
		{Encode(40, 5), []byte{31, 9}},
		{Encode(42, 0), []byte{42}},
		{Encode(1337, 5), []byte{31, 154, 10}},
		{Encode(3000000, 5), []byte{31, 161, 141, 183, 1}},
	}

	for _, testcase := range testcases {
		actual := testcase.actual.Bytes()
		expected := testcase.expected
		assert.Equal(t, actual, expected)
	}
}
示例#30
0
func TestToHeader(t *testing.T) {
	header := make(http.Header)
	header.Add("method", "get")
	header.Add("host", "example.com")
	header.Add(":authority", "example.com")
	header.Add("cookie", "a")
	header.Add("cookie", "b")

	headerList := HeaderList{
		{"method", "get"},
		{"host", "example.com"},
		{":authority", "example.com"},
		{"cookie", "a"},
		{"cookie", "b"},
	}
	actual := headerList.ToHeader()
	assert.Equal(t, actual, header)
}