message_test.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package ims
  2. import (
  3. "reflect"
  4. "testing"
  5. )
  6. func TestParseSIPResponseAndSplitIdentityHeaders(t *testing.T) {
  7. packet := []byte(
  8. "SIP/2.0 200 OK\r\n" +
  9. "Call-ID: register@example\r\n" +
  10. "CSeq: 2 REGISTER\r\n" +
  11. "P-Associated-URI: <sip:[email protected]>,\r\n" +
  12. " <tel:+8613800138000>\r\n" +
  13. "Service-Route: <sip:first.example;lr>\r\n" +
  14. "Service-Route: <sip:second.example;lr>\r\n" +
  15. "Content-Length: 4\r\n\r\nbody",
  16. )
  17. response, err := parseSIPResponse(packet)
  18. if err != nil {
  19. t.Fatalf("parseSIPResponse() error = %v", err)
  20. }
  21. if response.StatusCode != 200 || string(response.Body) != "body" {
  22. t.Fatalf("unexpected response: %#v", response)
  23. }
  24. identities := splitHeaderValues(response.values("P-Associated-URI"))
  25. wantIdentities := []string{
  26. "<sip:[email protected]>",
  27. "<tel:+8613800138000>",
  28. }
  29. if !reflect.DeepEqual(identities, wantIdentities) {
  30. t.Fatalf("identities = %#v, want %#v", identities, wantIdentities)
  31. }
  32. wantRoutes := []string{"<sip:first.example;lr>", "<sip:second.example;lr>"}
  33. if routes := splitHeaderValues(response.values("Service-Route")); !reflect.DeepEqual(routes, wantRoutes) {
  34. t.Fatalf("routes = %#v, want %#v", routes, wantRoutes)
  35. }
  36. }
  37. func TestParseSIPResponseRejectsIncompleteBody(t *testing.T) {
  38. _, err := parseSIPResponse([]byte("SIP/2.0 200 OK\r\nContent-Length: 4\r\n\r\nx"))
  39. if err == nil {
  40. t.Fatal("parseSIPResponse() error = nil, want incomplete body error")
  41. }
  42. }
  43. func TestParseSIPMessageRequestWithBinaryBody(t *testing.T) {
  44. body := []byte{0x01, 0x2a, 0x00, 0x00}
  45. packet := append([]byte(
  46. "MESSAGE sip:[email protected] SIP/2.0\r\n"+
  47. "Via: SIP/2.0/TCP proxy.example.test;branch=z9hG4bK1\r\n"+
  48. "From: <sip:[email protected]>;tag=a\r\n"+
  49. "To: <sip:[email protected]>\r\n"+
  50. "Call-ID: inbound-1\r\n"+
  51. "CSeq: 1 MESSAGE\r\n"+
  52. "Content-Type: application/vnd.3gpp.sms\r\n"+
  53. "Content-Length: 4\r\n\r\n",
  54. ), body...)
  55. message, err := parseSIPPacket(packet)
  56. if err != nil {
  57. t.Fatalf("parseSIPPacket: %v", err)
  58. }
  59. if message.Request == nil || message.Request.Method != "MESSAGE" ||
  60. message.Request.URI != "sip:[email protected]" ||
  61. !reflect.DeepEqual(message.Request.Body, body) {
  62. t.Fatalf("message = %#v", message)
  63. }
  64. }
  65. func TestSplitHeaderValuesPreservesQuotedCommas(t *testing.T) {
  66. values := splitHeaderValues([]string{
  67. `"Doe, Jane" <sip:[email protected]>, <sip:[email protected]>`,
  68. })
  69. want := []string{
  70. `"Doe, Jane" <sip:[email protected]>`,
  71. `<sip:[email protected]>`,
  72. }
  73. if !reflect.DeepEqual(values, want) {
  74. t.Fatalf("splitHeaderValues() = %#v, want %#v", values, want)
  75. }
  76. }