tshttpproxy_test.go 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright (c) 2021 Tailscale Inc & AUTHORS All rights reserved.
  2. // Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package tshttpproxy
  5. import (
  6. "net/url"
  7. "runtime"
  8. "strings"
  9. "testing"
  10. )
  11. func TestGetAuthHeaderNoResult(t *testing.T) {
  12. const proxyURL = "http://127.0.0.1:38274"
  13. u, err := url.Parse(proxyURL)
  14. if err != nil {
  15. t.Fatalf("can't parse %q: %v", proxyURL, err)
  16. }
  17. got, err := GetAuthHeader(u)
  18. if err != nil {
  19. t.Fatalf("can't get auth header value: %v", err)
  20. }
  21. if runtime.GOOS == "windows" && strings.HasPrefix(got, "Negotiate") {
  22. t.Logf("didn't get empty result, but got acceptable Windows Negotiate header")
  23. return
  24. }
  25. if got != "" {
  26. t.Fatalf("GetAuthHeader(%q) = %q; want empty string", proxyURL, got)
  27. }
  28. }
  29. func TestGetAuthHeaderBasicAuth(t *testing.T) {
  30. const proxyURL = "http://user:[email protected]:38274"
  31. const want = "Basic dXNlcjpwYXNzd29yZA=="
  32. u, err := url.Parse(proxyURL)
  33. if err != nil {
  34. t.Fatalf("can't parse %q: %v", proxyURL, err)
  35. }
  36. got, err := GetAuthHeader(u)
  37. if err != nil {
  38. t.Fatalf("can't get auth header value: %v", err)
  39. }
  40. if got != want {
  41. t.Fatalf("GetAuthHeader(%q) = %q; want %q", proxyURL, got, want)
  42. }
  43. }