alpn_test.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
  2. // SPDX-License-Identifier: MIT
  3. package extension
  4. import (
  5. "errors"
  6. "reflect"
  7. "testing"
  8. )
  9. func TestALPN(t *testing.T) {
  10. extension := ALPN{
  11. ProtocolNameList: []string{"http/1.1", "spdy/1", "spdy/2", "spdy/3"},
  12. }
  13. raw, err := extension.Marshal()
  14. if err != nil {
  15. t.Fatal(err)
  16. }
  17. newExtension := ALPN{}
  18. err = newExtension.Unmarshal(raw)
  19. if err != nil {
  20. t.Fatal(err)
  21. }
  22. if !reflect.DeepEqual(newExtension.ProtocolNameList, extension.ProtocolNameList) {
  23. t.Errorf("extensionALPN marshal: got %s expected %s", newExtension.ProtocolNameList, extension.ProtocolNameList)
  24. }
  25. }
  26. func TestALPNProtocolSelection(t *testing.T) {
  27. s, err := ALPNProtocolSelection([]string{"http/1.1", "spd/1"}, []string{"spd/1"})
  28. if err != nil {
  29. t.Fatal(err)
  30. }
  31. if s != "spd/1" {
  32. t.Errorf("expected: spd/1, got: %v", s)
  33. }
  34. _, err = ALPNProtocolSelection([]string{"http/1.1"}, []string{"spd/1"})
  35. if !errors.Is(err, errALPNNoAppProto) {
  36. t.Fatal("expected to fail negotiating an application protocol")
  37. }
  38. s, err = ALPNProtocolSelection([]string{"http/1.1", "spd/1"}, []string{})
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. if s != "" {
  43. t.Errorf("expected not to negotiate a protocol, got: %v", s)
  44. }
  45. }