Пример #1
0
func TestLineComments(t *testing.T) {
	is := is.New(t)
	l, err := parse.ParseLine(0, []byte(`* Key: "Value" // comments should be ignored`))
	is.NoErr(err)
	detail := l.Detail()
	is.OK(detail)
	is.Equal(detail.Key, "Key")
	is.Equal(detail.Value.Data, "Value")
	l, err = parse.ParseLine(0, []byte(`* Key: "Value" // comments should be ignored`))
	is.NoErr(err)
	is.Equal(string(l.Bytes), `* Key: "Value"`)
}
Пример #2
0
func TestLineDetail(t *testing.T) {
	is := is.New(t)
	l, err := parse.ParseLine(0, []byte(`* Key-Here: "Value"`))
	is.NoErr(err)
	detail := l.Detail()
	is.Equal(detail.Key, "Key-Here")
	is.Equal(detail.Value.Data, "Value")
}
Пример #3
0
func TestLineComments(t *testing.T) {
	is := is.New(t)
	l, err := parse.ParseLine(0, []byte(`* Key: "Value" // comments should be ignored`))
	is.NoErr(err)
	is.Equal(l.Comment, " comments should be ignored")
	detail := l.Detail()
	is.OK(detail)
	is.Equal(detail.Key, "Key")
	is.Equal(detail.Value.Data, "Value")
	l, err = parse.ParseLine(0, []byte(`* Key: "Value" // comments should be ignored`))
	is.NoErr(err)
	is.Equal(string(l.Bytes), `* Key: "Value"`)

	// if the comment contains a {placeholder}, grab it
	l, err = parse.ParseLine(0, []byte(`* Key: "Value" // will be stored as {id}`))
	is.NoErr(err)
	is.Equal(l.Comment, " will be stored as {id}")
	is.Equal(l.Capture(), "id")
}
Пример #4
0
func TestLinesReaderMutableBytes(t *testing.T) {
	is := is.New(t)

	lineText := "This line shouldn't change after parsing"
	mutableData := []byte(lineText)

	l, err := parse.ParseLine(0, mutableData)
	is.NoErr(err)

	mutableData[5] = 'w'

	is.Equal(lineText, string(l.Bytes))
}
Пример #5
0
func TestLinesReader(t *testing.T) {
	is := is.New(t)

	var lines parse.Lines

	l, err := parse.ParseLine(0, []byte("Line one "))
	is.NoErr(err)
	lines = append(lines, l)
	l, err = parse.ParseLine(1, []byte("Line two "))
	is.NoErr(err)
	lines = append(lines, l)
	l, err = parse.ParseLine(2, []byte("Line three"))
	is.NoErr(err)
	lines = append(lines, l)

	out, err := ioutil.ReadAll(lines.Reader())
	is.NoErr(err)
	is.Equal(string(out), `Line one 
Line two 
Line three`)

}
Пример #6
0
func TestLineParams(t *testing.T) {
	is := is.New(t)
	for i, line := range []string{
		"* ?key=value",
		"* `?key=value`",
		"* ?`key`=`value`",
	} {
		l, err := parse.ParseLine(i, []byte(line))
		is.NoErr(err)
		is.Equal(l.Type, parse.LineTypeParam)
		detail := l.Detail()
		is.OK(detail)
		is.Equal(detail.Key, "key")
		is.Equal(detail.Value.Data, "value")
	}
}
Пример #7
0
func TestParseLine(t *testing.T) {
	is := is.New(t)

	var tests = []struct {
		Src  string
		Type parse.LineType
	}{{
		Src:  "",
		Type: parse.LineTypePlain,
	}, {
		Src:  "Normal text is just considered plain.",
		Type: parse.LineTypePlain,
	}, {
		Src:  "# Heading",
		Type: parse.LineTypeGroupHeading,
	}, {
		Src:  "## `POST /something`",
		Type: parse.LineTypeRequest,
	}, {
		Src:  "## POST /echo",
		Type: parse.LineTypeRequest,
	}, {
		Src:  "### Example request",
		Type: parse.LineTypePlain,
	}, {
		Src:  "* `Detail`: `123`",
		Type: parse.LineTypeDetail,
	}, {
		Src:  "* `?param=value`",
		Type: parse.LineTypeParam,
	}, {
		Src:  "  * `Detail`: `123`",
		Type: parse.LineTypeDetail,
	}, {
		Src:  "  * `?param=value`",
		Type: parse.LineTypeParam,
	}, {
		Src:  "* ?param=value",
		Type: parse.LineTypeParam,
	}, {
		Src:  "* Cookie: name=value",
		Type: parse.LineTypeDetail,
	}, {
		Src:  `* Set-Cookie: "another=true"`,
		Type: parse.LineTypeDetail,
	}, {
		Src:  "===",
		Type: parse.LineTypeSeparator,
	}, {
		Src:  "====",
		Type: parse.LineTypeSeparator,
	}, {
		Src:  "=====",
		Type: parse.LineTypeSeparator,
	}, {
		Src:  "---",
		Type: parse.LineTypeSeparator,
	}, {
		Src:  "----",
		Type: parse.LineTypeSeparator,
	}, {
		Src:  "-----",
		Type: parse.LineTypeSeparator,
	}}
	for i, test := range tests {
		l, err := parse.ParseLine(i, []byte(test.Src))
		is.NoErr(err)
		is.Equal(l.Type, test.Type)
		is.Equal(l.Bytes, []byte(test.Src))
		is.Equal(l.Number, i)
	}

}