basic_test.go 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2020 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package authutil
  5. import (
  6. "net/http"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. )
  10. func TestDecodeBasic(t *testing.T) {
  11. tests := []struct {
  12. name string
  13. header http.Header
  14. expUsername string
  15. expPassword string
  16. }{
  17. {
  18. name: "no header",
  19. },
  20. {
  21. name: "no authorization",
  22. header: http.Header{
  23. "Content-Type": []string{"text/plain"},
  24. },
  25. },
  26. {
  27. name: "malformed value",
  28. header: http.Header{
  29. "Authorization": []string{"Basic"},
  30. },
  31. },
  32. {
  33. name: "not basic",
  34. header: http.Header{
  35. "Authorization": []string{"Digest dummy"},
  36. },
  37. },
  38. {
  39. name: "bad encoding",
  40. header: http.Header{
  41. "Authorization": []string{"Basic not_base64"},
  42. },
  43. },
  44. {
  45. name: "only has username",
  46. header: http.Header{
  47. "Authorization": []string{"Basic dXNlcm5hbWU="},
  48. },
  49. expUsername: "username",
  50. },
  51. {
  52. name: "has username and password",
  53. header: http.Header{
  54. "Authorization": []string{"Basic dXNlcm5hbWU6cGFzc3dvcmQ="},
  55. },
  56. expUsername: "username",
  57. expPassword: "password",
  58. },
  59. }
  60. for _, test := range tests {
  61. t.Run(test.name, func(t *testing.T) {
  62. username, password := DecodeBasic(test.header)
  63. assert.Equal(t, test.expUsername, username)
  64. assert.Equal(t, test.expPassword, password)
  65. })
  66. }
  67. }