github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/webbrowser/mock_test.go (about) 1 package webbrowser 2 3 import ( 4 "context" 5 "net/http" 6 "net/http/httptest" 7 "testing" 8 ) 9 10 func TestMockLauncher(t *testing.T) { 11 s := httptest.NewServer(http.HandlerFunc(func(resp http.ResponseWriter, req *http.Request) { 12 resp.Header().Set("Content-Length", "0") 13 switch req.URL.Path { 14 case "/standard-redirect-source": 15 resp.Header().Set("Location", "/standard-redirect-target") 16 resp.WriteHeader(302) 17 case "/custom-redirect-source": 18 resp.Header().Set("X-Redirect-To", "/custom-redirect-target") 19 resp.WriteHeader(200) 20 case "/error": 21 resp.WriteHeader(500) 22 default: 23 resp.WriteHeader(200) 24 } 25 })) 26 defer s.Close() 27 28 t.Run("no redirects", func(t *testing.T) { 29 l := NewMockLauncher(context.Background()) 30 err := l.OpenURL(s.URL) 31 if err != nil { 32 t.Fatal(err) 33 } 34 l.Wait() // Let the async work complete 35 if got, want := len(l.Responses), 1; got != want { 36 t.Fatalf("wrong number of responses %d; want %d", got, want) 37 } 38 if got, want := l.Responses[0].Request.URL.Path, ""; got != want { 39 t.Fatalf("wrong request URL %q; want %q", got, want) 40 } 41 }) 42 t.Run("error", func(t *testing.T) { 43 l := NewMockLauncher(context.Background()) 44 err := l.OpenURL(s.URL + "/error") 45 if err != nil { 46 // Th is kind of error is supposed to happen asynchronously, so we 47 // should not see it here. 48 t.Fatal(err) 49 } 50 l.Wait() // Let the async work complete 51 if got, want := len(l.Responses), 1; got != want { 52 t.Fatalf("wrong number of responses %d; want %d", got, want) 53 } 54 if got, want := l.Responses[0].Request.URL.Path, "/error"; got != want { 55 t.Fatalf("wrong request URL %q; want %q", got, want) 56 } 57 if got, want := l.Responses[0].StatusCode, 500; got != want { 58 t.Fatalf("wrong response status %d; want %d", got, want) 59 } 60 }) 61 t.Run("standard redirect", func(t *testing.T) { 62 l := NewMockLauncher(context.Background()) 63 err := l.OpenURL(s.URL + "/standard-redirect-source") 64 if err != nil { 65 t.Fatal(err) 66 } 67 l.Wait() // Let the async work complete 68 if got, want := len(l.Responses), 2; got != want { 69 t.Fatalf("wrong number of responses %d; want %d", got, want) 70 } 71 if got, want := l.Responses[0].Request.URL.Path, "/standard-redirect-source"; got != want { 72 t.Fatalf("wrong request 0 URL %q; want %q", got, want) 73 } 74 if got, want := l.Responses[1].Request.URL.Path, "/standard-redirect-target"; got != want { 75 t.Fatalf("wrong request 1 URL %q; want %q", got, want) 76 } 77 }) 78 t.Run("custom redirect", func(t *testing.T) { 79 l := NewMockLauncher(context.Background()) 80 err := l.OpenURL(s.URL + "/custom-redirect-source") 81 if err != nil { 82 t.Fatal(err) 83 } 84 l.Wait() // Let the async work complete 85 if got, want := len(l.Responses), 2; got != want { 86 t.Fatalf("wrong number of responses %d; want %d", got, want) 87 } 88 if got, want := l.Responses[0].Request.URL.Path, "/custom-redirect-source"; got != want { 89 t.Fatalf("wrong request 0 URL %q; want %q", got, want) 90 } 91 if got, want := l.Responses[1].Request.URL.Path, "/custom-redirect-target"; got != want { 92 t.Fatalf("wrong request 1 URL %q; want %q", got, want) 93 } 94 }) 95 }