mirror of
https://github.com/MengMengCode/VoCat.git
synced 2026-08-18 22:03:44 +08:00
82 lines
2.6 KiB
Go
82 lines
2.6 KiB
Go
package ims
|
|
|
|
import (
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestParseSIPResponseAndSplitIdentityHeaders(t *testing.T) {
|
|
packet := []byte(
|
|
"SIP/2.0 200 OK\r\n" +
|
|
"Call-ID: register@example\r\n" +
|
|
"CSeq: 2 REGISTER\r\n" +
|
|
"P-Associated-URI: <sip:[email protected]>,\r\n" +
|
|
" <tel:+8613800138000>\r\n" +
|
|
"Service-Route: <sip:first.example;lr>\r\n" +
|
|
"Service-Route: <sip:second.example;lr>\r\n" +
|
|
"Content-Length: 4\r\n\r\nbody",
|
|
)
|
|
response, err := parseSIPResponse(packet)
|
|
if err != nil {
|
|
t.Fatalf("parseSIPResponse() error = %v", err)
|
|
}
|
|
if response.StatusCode != 200 || string(response.Body) != "body" {
|
|
t.Fatalf("unexpected response: %#v", response)
|
|
}
|
|
identities := splitHeaderValues(response.values("P-Associated-URI"))
|
|
wantIdentities := []string{
|
|
"<sip:[email protected]>",
|
|
"<tel:+8613800138000>",
|
|
}
|
|
if !reflect.DeepEqual(identities, wantIdentities) {
|
|
t.Fatalf("identities = %#v, want %#v", identities, wantIdentities)
|
|
}
|
|
wantRoutes := []string{"<sip:first.example;lr>", "<sip:second.example;lr>"}
|
|
if routes := splitHeaderValues(response.values("Service-Route")); !reflect.DeepEqual(routes, wantRoutes) {
|
|
t.Fatalf("routes = %#v, want %#v", routes, wantRoutes)
|
|
}
|
|
}
|
|
|
|
func TestParseSIPResponseRejectsIncompleteBody(t *testing.T) {
|
|
_, err := parseSIPResponse([]byte("SIP/2.0 200 OK\r\nContent-Length: 4\r\n\r\nx"))
|
|
if err == nil {
|
|
t.Fatal("parseSIPResponse() error = nil, want incomplete body error")
|
|
}
|
|
}
|
|
|
|
func TestParseSIPMessageRequestWithBinaryBody(t *testing.T) {
|
|
body := []byte{0x01, 0x2a, 0x00, 0x00}
|
|
packet := append([]byte(
|
|
"MESSAGE sip:[email protected] SIP/2.0\r\n"+
|
|
"Via: SIP/2.0/TCP proxy.example.test;branch=z9hG4bK1\r\n"+
|
|
"From: <sip:[email protected]>;tag=a\r\n"+
|
|
"To: <sip:[email protected]>\r\n"+
|
|
"Call-ID: inbound-1\r\n"+
|
|
"CSeq: 1 MESSAGE\r\n"+
|
|
"Content-Type: application/vnd.3gpp.sms\r\n"+
|
|
"Content-Length: 4\r\n\r\n",
|
|
), body...)
|
|
message, err := parseSIPPacket(packet)
|
|
if err != nil {
|
|
t.Fatalf("parseSIPPacket: %v", err)
|
|
}
|
|
if message.Request == nil || message.Request.Method != "MESSAGE" ||
|
|
message.Request.URI != "sip:[email protected]" ||
|
|
!reflect.DeepEqual(message.Request.Body, body) {
|
|
t.Fatalf("message = %#v", message)
|
|
}
|
|
}
|
|
|
|
func TestSplitHeaderValuesPreservesQuotedCommas(t *testing.T) {
|
|
values := splitHeaderValues([]string{
|
|
`"Doe, Jane" <sip:[email protected]>, <sip:[email protected]>`,
|
|
})
|
|
want := []string{
|
|
`"Doe, Jane" <sip:[email protected]>`,
|
|
`<sip:[email protected]>`,
|
|
}
|
|
if !reflect.DeepEqual(values, want) {
|
|
t.Fatalf("splitHeaderValues() = %#v, want %#v", values, want)
|
|
}
|
|
}
|