protocol/rtsp: wrote test ReadResponse, but work in progress

This commit is contained in:
Saxon 2019-04-29 18:21:59 +09:30
parent 449869a4ac
commit d2b76fab36
2 changed files with 87 additions and 9 deletions

View File

@ -148,16 +148,21 @@ func ReadResponse(r io.Reader) (*Response, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
text := scanner.Text()
parts := strings.SplitN(scanner.Text(), ":", 2) fmt.Printf("text: %v\n", text)
resp.Header.Add(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])) parts := strings.SplitN(text, ":", 2)
fmt.Printf("text: %v\n", parts)
if len(parts) < 2 {
break
} }
p1 := strings.TrimSpace(parts[0])
p2 := strings.TrimSpace(parts[1])
fmt.Printf("p1: %v\n,p2: %v\n", p1, p2)
resp.Header.Add(p1, p2)
}
fmt.Printf("header: %v", resp.Header)
// Get the content length from the header. // Get the content length from the header.
resp.ContentLength, err = strconv.Atoi(resp.Header.Get("Content-Length")) resp.ContentLength, _ = strconv.Atoi(resp.Header.Get("Content-Length"))
if err != nil {
return nil, err
}
resp.Body = closer{r} resp.Body = closer{r}
return resp, nil return resp, nil

View File

@ -1,6 +1,6 @@
/* /*
NAME NAME
rtsp_test.go 0x r,tsp_test.go
DESCRIPTION DESCRIPTION
rtsp_test.go provides a test to check functionality provided in rtsp.go. rtsp_test.go provides a test to check functionality provided in rtsp.go.
@ -27,6 +27,7 @@ LICENSE
package rtsp package rtsp
import ( import (
"bytes"
"errors" "errors"
"fmt" "fmt"
"io" "io"
@ -269,3 +270,75 @@ func rmSeqNum(s []string) ([]string, bool) {
} }
return nil, false return nil, false
} }
func TestReadResponse(t *testing.T) {
input := []byte{
0x52, 0x54, 0x53, 0x50, 0x2f, 0x31, 0x2e, 0x30, 0x20, 0x32, 0x30, 0x30, 0x20, 0x4f, 0x4b, 0x0d, // |RTSP/1.0 200 OK.|
0x0a, 0x43, 0x53, 0x65, 0x71, 0x3a, 0x20, 0x32, 0x0d, 0x0a, 0x44, 0x61, 0x74, 0x65, 0x3a, 0x20, // |.CSeq: 2..Date: |
0x57, 0x65, 0x64, 0x2c, 0x20, 0x4a, 0x61, 0x6e, 0x20, 0x32, 0x31, 0x20, 0x31, 0x39, 0x37, 0x30, // |Wed, Jan 21 1970|
0x20, 0x30, 0x32, 0x3a, 0x33, 0x37, 0x3a, 0x31, 0x34, 0x20, 0x47, 0x4d, 0x54, 0x0d, 0x0a, 0x50, // | 02:37:14 GMT..P|
0x75, 0x62, 0x6c, 0x69, 0x63, 0x3a, 0x20, 0x4f, 0x50, 0x54, 0x49, 0x4f, 0x4e, 0x53, 0x2c, 0x20, // |ublic: OPTIONS, |
0x44, 0x45, 0x53, 0x43, 0x52, 0x49, 0x42, 0x45, 0x2c, 0x20, 0x53, 0x45, 0x54, 0x55, 0x50, 0x2c, // |DESCRIBE, SETUP,|
0x20, 0x54, 0x45, 0x41, 0x52, 0x44, 0x4f, 0x57, 0x4e, 0x2c, 0x20, 0x50, 0x4c, 0x41, 0x59, 0x2c, // | TEARDOWN, PLAY,|
0x20, 0x47, 0x45, 0x54, 0x5f, 0x50, 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x2c, 0x20, // | GET_PARAMETER, |
0x53, 0x45, 0x54, 0x5f, 0x50, 0x41, 0x52, 0x41, 0x4d, 0x45, 0x54, 0x45, 0x52, 0x0d, 0x0a, 0x0d, // |SET_PARAMETER...|
0x0a,
}
fmt.Printf("string: %v", string(input))
expect := Response{
Proto: "RTSP",
ProtoMajor: 1,
ProtoMinor: 0,
StatusCode: 200,
ContentLength: 0,
Header: map[string][]string{
"CSeq": []string{"2"},
"Date": []string{"Wed, Jan 21 1970 02:37:14 GMT"},
"Public": []string{"OPTIONS, DESCRIBE, SETUP, TEARDOWN, PLAY, GET_PARAMETER, SET_PARAMETER"},
},
}
got, err := ReadResponse(bytes.NewReader(input))
fmt.Printf("resp: %v", got.String())
if err != nil {
t.Fatalf("should not have got error: %v", err)
}
if !respEqual(*got, expect) {
t.Errorf("did not get expected result.\nGot: %+v\n Want: %+v\n", got, expect)
}
}
func respEqual(got, want Response) bool {
for i, f := range [][2]interface{}{
{got.Proto, want.Proto},
{got.ProtoMajor, want.ProtoMajor},
{got.ProtoMinor, want.ProtoMinor},
{got.StatusCode, want.StatusCode},
{got.ContentLength, want.ContentLength},
} {
if f[0] != f[1] {
fmt.Printf("at i: %v", i)
return false
}
}
if len(got.Header) != len(want.Header) {
fmt.Println("here1")
return false
}
for k, v := range got.Header {
if len(v) != len(want.Header[k]) {
fmt.Printf("len(v): %v, len(want.Header): %v", len(v), len(want.Header[k]))
fmt.Printf("k: %v", k)
return false
}
for i, _v := range v {
if _v != want.Header[k][i] {
fmt.Printf("k: %v, v: %v, _v: %v", k, v, _v)
return false
}
}
}
return true
}