From d2b76fab36269b4c49b3cc9ae892aeac6afeae45 Mon Sep 17 00:00:00 2001 From: Saxon Date: Mon, 29 Apr 2019 18:21:59 +0930 Subject: [PATCH] protocol/rtsp: wrote test ReadResponse, but work in progress --- protocol/rtsp/rtsp.go | 21 +++++++---- protocol/rtsp/rtsp_test.go | 75 +++++++++++++++++++++++++++++++++++++- 2 files changed, 87 insertions(+), 9 deletions(-) diff --git a/protocol/rtsp/rtsp.go b/protocol/rtsp/rtsp.go index 029a57f1..deaefb50 100644 --- a/protocol/rtsp/rtsp.go +++ b/protocol/rtsp/rtsp.go @@ -148,16 +148,21 @@ func ReadResponse(r io.Reader) (*Response, error) { if err != nil { return nil, err } - - parts := strings.SplitN(scanner.Text(), ":", 2) - resp.Header.Add(strings.TrimSpace(parts[0]), strings.TrimSpace(parts[1])) + text := scanner.Text() + fmt.Printf("text: %v\n", text) + 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. - resp.ContentLength, err = strconv.Atoi(resp.Header.Get("Content-Length")) - if err != nil { - return nil, err - } + resp.ContentLength, _ = strconv.Atoi(resp.Header.Get("Content-Length")) resp.Body = closer{r} return resp, nil diff --git a/protocol/rtsp/rtsp_test.go b/protocol/rtsp/rtsp_test.go index f1a2257b..ea80ec2f 100644 --- a/protocol/rtsp/rtsp_test.go +++ b/protocol/rtsp/rtsp_test.go @@ -1,6 +1,6 @@ /* NAME - rtsp_test.go + 0x r,tsp_test.go DESCRIPTION rtsp_test.go provides a test to check functionality provided in rtsp.go. @@ -27,6 +27,7 @@ LICENSE package rtsp import ( + "bytes" "errors" "fmt" "io" @@ -269,3 +270,75 @@ func rmSeqNum(s []string) ([]string, bool) { } 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 +}