helpers_test.go 955 B

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. package login
  2. import (
  3. "testing"
  4. "github.com/google/go-cmp/cmp"
  5. )
  6. func TestMustSetQuery(t *testing.T) {
  7. for _, c := range []struct {
  8. name string
  9. u string
  10. kvs []string
  11. expect string
  12. }{
  13. {
  14. name: "no query",
  15. u: "/a/b",
  16. kvs: []string{
  17. "k1", "v1",
  18. "k2", "v2",
  19. },
  20. expect: "/a/b?k1=v1&k2=v2",
  21. },
  22. {
  23. name: "has query",
  24. u: "/a/b?a=1",
  25. kvs: []string{
  26. "k1", "v1",
  27. "k2", "v2",
  28. },
  29. expect: "/a/b?a=1&k1=v1&k2=v2",
  30. },
  31. {
  32. name: "has same query",
  33. u: "/a/b?a=1&k2=v22",
  34. kvs: []string{
  35. "k1", "v1",
  36. "k2", "v2",
  37. },
  38. expect: "/a/b?a=1&k1=v1&k2=v2",
  39. },
  40. {
  41. name: "full url",
  42. u: "https://example.com/a/b?a=1&k2=v22",
  43. kvs: []string{
  44. "k1", "v1",
  45. "k2", "v2",
  46. },
  47. expect: "https://example.com/a/b?a=1&k1=v1&k2=v2",
  48. },
  49. } {
  50. if diff := cmp.Diff(c.expect, MustSetQuery(c.u, c.kvs...)); diff != "" {
  51. t.Fatalf("%s: %s\n", c.name, diff)
  52. }
  53. }
  54. }