github.com/cnotch/ipchub@v1.1.0/av/format/rtsp/request_test.go (about) 1 // Copyright (c) 2019,CAOHONGJU All rights reserved. 2 // Use of this source code is governed by a MIT-style 3 // license that can be found in the LICENSE file. 4 5 package rtsp 6 7 import ( 8 "bufio" 9 "bytes" 10 "net/url" 11 "reflect" 12 "strings" 13 "testing" 14 ) 15 16 func TestReadRequest(t *testing.T) { 17 tests := requestTestCases() 18 for _, tt := range tests { 19 t.Run(tt.name, func(t *testing.T) { 20 got, err := ReadRequest(bufio.NewReader(bytes.NewBufferString(tt.str))) 21 if err != nil { 22 t.Errorf("ReadRequest() error = %v", err) 23 return 24 } 25 if !reflect.DeepEqual(got, tt.req) { 26 t.Errorf("ReadRequest() = %v, want %v", got, tt.req) 27 } 28 }) 29 } 30 } 31 32 func TestRequest_Write(t *testing.T) { 33 tests := requestTestCases() 34 for _, tt := range tests { 35 t.Run(tt.name, func(t *testing.T) { 36 buf := bytes.NewBuffer(make([]byte, 0, 1024)) 37 err := tt.req.Write(buf) 38 if err != nil { 39 t.Errorf("Request.Write() error = %v", err) 40 return 41 } 42 got := string(buf.Bytes()) 43 44 if got != tt.str { 45 t.Errorf("Request.Write() = %v, want %v", got, tt.str) 46 } 47 }) 48 } 49 } 50 51 type requestTestCase struct { 52 name string 53 str string 54 req *Request 55 } 56 57 func requestTestCases() []requestTestCase { 58 var testCases []requestTestCase 59 60 var str = `OPTIONS * RTSP/1.0 61 CSeq: 1 62 Proxy-Require: gzipped-messages 63 Require: implicit-play 64 65 ` 66 str = strings.Replace(str, "\n", "\r\n", -1) 67 var header = make(Header) 68 header.Set("CSeq", "1") 69 header.Set("Require", "implicit-play") 70 header.Set("Proxy-Require", "gzipped-messages") 71 req := new(Request) 72 req.Method = "OPTIONS" 73 req.URL, _ = url.Parse("*") 74 req.Proto = "RTSP/1.0" 75 req.Header = header 76 77 testCases = append(testCases, requestTestCase{"Single Value", str, req}) 78 79 str = `DESCRIBE rtsp://server.example.com/fizzle/foo RTSP/1.0 80 CSeq: 1 81 Public: DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE 82 83 ` 84 str = strings.Replace(str, "\n", "\r\n", -1) 85 header = make(Header) 86 header.Set("CSeq", "1") 87 header.Set("Public", "DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE") 88 // header.Add("Public", "DESCRIBE") 89 // header.Add("Public", "SETUP") 90 // header.Add("Public", "TEARDOWN") 91 // header.Add("Public", "PLAY") 92 // header.Add("Public", "PAUSE") 93 94 req = new(Request) 95 req.Method = "DESCRIBE" 96 req.URL, _ = url.Parse("rtsp://server.example.com/fizzle/foo") 97 req.Proto = "RTSP/1.0" 98 req.Header = header 99 100 testCases = append(testCases, requestTestCase{"Multi Value", str, req}) 101 102 str = `GET_PARAMETER rtsp://example.com/fizzle/foo RTSP/1.0 103 CSeq: 431 104 Content-Length: 15 105 Content-Type: text/parameters 106 Session: 12345678 107 108 123456789012345` 109 str = strings.Replace(str, "\n", "\r\n", -1) 110 header = make(Header) 111 header.Set("CSeq", "431") 112 header.Set("Content-Type", "text/parameters") 113 header.Set("Session", "12345678") 114 header.Set("Content-Length", "15") 115 116 req = new(Request) 117 req.Method = "GET_PARAMETER" 118 req.URL, _ = url.Parse("rtsp://example.com/fizzle/foo") 119 req.Proto = "RTSP/1.0" 120 req.Header = header 121 req.Body = "123456789012345" 122 123 testCases = append(testCases, requestTestCase{"With Body", str, req}) 124 125 return testCases 126 }