token_test.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package token
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/url"
  6. "testing"
  7. )
  8. func TestHandleRedirects_AttachOrgToken(t *testing.T) {
  9. req, _ := http.NewRequest("GET", "http://example.com/cdn-cgi/access/login", nil)
  10. via := []*http.Request{}
  11. orgToken := "orgTokenValue"
  12. _ = handleRedirects(req, via, orgToken)
  13. // Check if the orgToken cookie is attached
  14. cookies := req.Cookies()
  15. found := false
  16. for _, cookie := range cookies {
  17. if cookie.Name == tokenCookie && cookie.Value == orgToken {
  18. found = true
  19. break
  20. }
  21. }
  22. if !found {
  23. t.Errorf("OrgToken cookie not attached to the request.")
  24. }
  25. }
  26. func TestHandleRedirects_AttachAppSessionCookie(t *testing.T) {
  27. req, _ := http.NewRequest("GET", "http://example.com/cdn-cgi/access/authorized", nil)
  28. via := []*http.Request{
  29. {
  30. URL: &url.URL{Path: "/cdn-cgi/access/login"},
  31. Response: &http.Response{
  32. Header: http.Header{"Set-Cookie": {"CF_AppSession=appSessionValue"}},
  33. },
  34. },
  35. }
  36. orgToken := "orgTokenValue"
  37. err := handleRedirects(req, via, orgToken)
  38. // Check if the appSessionCookie is attached to the request
  39. cookies := req.Cookies()
  40. found := false
  41. for _, cookie := range cookies {
  42. if cookie.Name == appSessionCookie && cookie.Value == "appSessionValue" {
  43. found = true
  44. break
  45. }
  46. }
  47. if !found {
  48. t.Errorf("AppSessionCookie not attached to the request.")
  49. }
  50. if err != nil {
  51. t.Errorf("Expected no error, got %v", err)
  52. }
  53. }
  54. func TestHandleRedirects_StopAtAuthorizedEndpoint(t *testing.T) {
  55. req, _ := http.NewRequest("GET", "http://example.com/cdn-cgi/access/authorized", nil)
  56. via := []*http.Request{
  57. {
  58. URL: &url.URL{Path: "other"},
  59. },
  60. {
  61. URL: &url.URL{Path: AccessAuthorizedWorkerPath},
  62. },
  63. }
  64. orgToken := "orgTokenValue"
  65. err := handleRedirects(req, via, orgToken)
  66. // Check if ErrUseLastResponse is returned
  67. if err != http.ErrUseLastResponse {
  68. t.Errorf("Expected ErrUseLastResponse, got %v", err)
  69. }
  70. }
  71. func TestJwtPayloadUnmarshal_AudAsString(t *testing.T) {
  72. jwt := `{"aud":"7afbdaf987054f889b3bdd0d29ebfcd2"}`
  73. var payload jwtPayload
  74. if err := json.Unmarshal([]byte(jwt), &payload); err != nil {
  75. t.Errorf("Expected no error, got %v", err)
  76. }
  77. if len(payload.Aud) != 1 || payload.Aud[0] != "7afbdaf987054f889b3bdd0d29ebfcd2" {
  78. t.Errorf("Expected aud to be 7afbdaf987054f889b3bdd0d29ebfcd2, got %v", payload.Aud)
  79. }
  80. }
  81. func TestJwtPayloadUnmarshal_AudAsSlice(t *testing.T) {
  82. jwt := `{"aud":["7afbdaf987054f889b3bdd0d29ebfcd2", "f835c0016f894768976c01e076844efe"]}`
  83. var payload jwtPayload
  84. if err := json.Unmarshal([]byte(jwt), &payload); err != nil {
  85. t.Errorf("Expected no error, got %v", err)
  86. }
  87. if len(payload.Aud) != 2 || payload.Aud[0] != "7afbdaf987054f889b3bdd0d29ebfcd2" || payload.Aud[1] != "f835c0016f894768976c01e076844efe" {
  88. t.Errorf("Expected aud to be [7afbdaf987054f889b3bdd0d29ebfcd2, f835c0016f894768976c01e076844efe], got %v", payload.Aud)
  89. }
  90. }
  91. func TestJwtPayloadUnmarshal_FailsWhenAudIsInt(t *testing.T) {
  92. jwt := `{"aud":123}`
  93. var payload jwtPayload
  94. err := json.Unmarshal([]byte(jwt), &payload)
  95. wantErr := "aud field is not a string or an array of strings"
  96. if err.Error() != wantErr {
  97. t.Errorf("Expected %v, got %v", wantErr, err)
  98. }
  99. }
  100. func TestJwtPayloadUnmarshal_FailsWhenAudIsArrayOfInts(t *testing.T) {
  101. jwt := `{"aud": [999, 123] }`
  102. var payload jwtPayload
  103. err := json.Unmarshal([]byte(jwt), &payload)
  104. wantErr := "aud array contains non-string elements"
  105. if err.Error() != wantErr {
  106. t.Errorf("Expected %v, got %v", wantErr, err)
  107. }
  108. }
  109. func TestJwtPayloadUnmarshal_FailsWhenAudIsOmitted(t *testing.T) {
  110. jwt := `{}`
  111. var payload jwtPayload
  112. err := json.Unmarshal([]byte(jwt), &payload)
  113. wantErr := "aud field is not a string or an array of strings"
  114. if err.Error() != wantErr {
  115. t.Errorf("Expected %v, got %v", wantErr, err)
  116. }
  117. }