github.com/cnotch/ipchub@v1.1.0/av/format/rtsp/header_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 "reflect" 11 "strings" 12 "testing" 13 ) 14 15 func TestReadHeader(t *testing.T) { 16 tests := headerTestCases() 17 for _, tt := range tests { 18 t.Run(tt.name, func(t *testing.T) { 19 got, err := ReadHeader(bufio.NewReader(bytes.NewBufferString(tt.str))) 20 if err != nil { 21 t.Errorf("ReadHeader() error = %v", err) 22 return 23 } 24 if !reflect.DeepEqual(got, tt.header) { 25 t.Errorf("ReadHeader() = %v, want %v", got, tt.header) 26 } 27 }) 28 } 29 } 30 31 func TestHeader_Write(t *testing.T) { 32 tests := headerTestCases() 33 for _, tt := range tests { 34 t.Run(tt.name, func(t *testing.T) { 35 buf := bytes.NewBuffer(make([]byte, 0, 1024)) 36 37 err := tt.header.Write(buf) 38 got := string(buf.Bytes()) 39 if err != nil { 40 t.Errorf("Header.Write() error = %v", err) 41 return 42 } 43 if got != tt.str { 44 t.Errorf("Header.Write() = %v, want %v", got, tt.str) 45 } 46 }) 47 } 48 } 49 50 type headerTestCase struct { 51 name string 52 str string 53 header Header 54 } 55 56 func headerTestCases() []headerTestCase { 57 var testCases []headerTestCase 58 59 var str = `CSeq: 1 60 Proxy-Require: gzipped-messages 61 Require: implicit-play 62 63 ` 64 str = strings.Replace(str, "\n", "\r\n", -1) 65 var header = make(Header) 66 header.Set("CSeq", "1") 67 header.Set("Require", "implicit-play") 68 header.Set("Proxy-Require", "gzipped-messages") 69 70 testCases = append(testCases, headerTestCase{"Single Value", str, header}) 71 72 str = `CSeq: 1 73 Public: DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE 74 75 ` 76 str = strings.Replace(str, "\n", "\r\n", -1) 77 header = make(Header) 78 header.Set("CSeq", "1") 79 header.Set("Public", "DESCRIBE, SETUP, TEARDOWN, PLAY, PAUSE") 80 // header.Add("Public", "DESCRIBE") 81 // header.Add("Public", "SETUP") 82 // header.Add("Public", "TEARDOWN") 83 // header.Add("Public", "PLAY") 84 // header.Add("Public", "PAUSE") 85 86 testCases = append(testCases, headerTestCase{"Multi Value", str, header}) 87 88 return testCases 89 }