basic_test.go 1.3 KB

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