sip_test.go (2567B)
1 package sip 2 3 import ( 4 "io" 5 "net/textproto" 6 "os" 7 "strings" 8 "testing" 9 ) 10 11 func TestWriteRequest(t *testing.T) { 12 header := make(textproto.MIMEHeader) 13 header.Set("Call-ID", "a84b4c76e66710@pc33.example.com") 14 header.Set("CSeq", "314159 "+MethodInvite) 15 header.Set("Contact", "<sip:alice@pc33.example.com>") 16 req := &Request{ 17 Method: MethodInvite, 18 URI: "sip:bob@example.com", 19 To: Address{Name: "Bob", URI: URI{Scheme: "sip", Opaque: "bob@example.com"}}, 20 From: Address{Name: "Alice", URI: URI{Scheme: "sip", Opaque: "alice@example.com"}}, 21 Via: Via{Address: "pc33.example.com", Branch: "776asdhds"}, 22 Header: header, 23 } 24 25 if _, err := WriteRequest(io.Discard, req); err != nil { 26 t.Fatalf("write request: %v", err) 27 } 28 } 29 30 func TestAddress(t *testing.T) { 31 var tests = []struct { 32 name string 33 addr string 34 want string 35 }{ 36 {"bare", "sip:test@example.com", "<sip:test@example.com>"}, 37 {"basic", "<sip:test@example.com>", "<sip:test@example.com>"}, 38 {"bare tag", "sip:+1234@example.com;tag=887s", "<sip:+1234@example.com>;tag=887s"}, 39 {"tag", "<sip:test@example.com>;tag=1234", "<sip:test@example.com>;tag=1234"}, 40 {"name", "Oliver <sip:test@example.com>", "Oliver <sip:test@example.com>"}, 41 {"name tag", "Oliver <sip:test@example.com>;tag=1234", "Oliver <sip:test@example.com>;tag=1234"}, 42 } 43 44 for _, tt := range tests { 45 t.Run(tt.name, func(t *testing.T) { 46 got, err := ParseAddress(tt.addr) 47 if err != nil { 48 t.Fatalf("parse %q: %v", tt.addr, err) 49 } 50 if got.String() != tt.want { 51 t.Fatalf("ParseAddress(%q) = %s, want %s", tt.addr, got, tt.want) 52 } 53 }) 54 } 55 } 56 57 func TestReadRequest(t *testing.T) { 58 f, err := os.Open("testdata/invite") 59 if err != nil { 60 t.Fatal(err) 61 } 62 defer f.Close() 63 _, err = ReadRequest(f) 64 if err != nil { 65 t.Fatal("read request:", err) 66 } 67 68 } 69 70 func TestResponse(t *testing.T) { 71 raw := `SIP/2.0 200 OK 72 Via: SIP/2.0/UDP server10.example.com 73 ;branch=z9hG4bKnashds8;received=192.0.2.3 74 Via: SIP/2.0/UDP bigbox3.site3.example.com 75 ;branch=z9hG4bK77ef4c2312983.1;received=192.0.2.2 76 Via: SIP/2.0/UDP pc33.example.com 77 ;branch=z9hG4bK776asdhds ;received=192.0.2.1 78 To: Bob <sip:bob@example.com>;tag=a6c85cf 79 From: Alice <sip:alice@example.com>;tag=1928301774 80 Call-ID: a84b4c76e66710@pc33.example.com 81 CSeq: 314159 INVITE 82 Contact: <sip:bob@192.0.2.4> 83 Content-Type: application/sdp 84 Content-Length: 131 85 86 ...` 87 msg, err := readMessage(strings.NewReader(raw)) 88 if err != nil { 89 t.Fatal("read message:", err) 90 } 91 _, err = parseResponse(msg) 92 if err != nil { 93 t.Fatalf("parse response: %v", err) 94 } 95 }