|
| 1 | +package tunnel |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "flag" |
| 6 | + "fmt" |
| 7 | + "github.com/cloudflare/cloudflared/cmd/cloudflared/cliutil" |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/urfave/cli/v2" |
| 10 | + "net/http" |
| 11 | + "net/http/httptest" |
| 12 | + "testing" |
| 13 | +) |
| 14 | + |
| 15 | +func makeContext(i int, serverUrl string) *cli.Context { |
| 16 | + flagSet := flag.NewFlagSet(fmt.Sprintf("test%d", i), flag.PanicOnError) |
| 17 | + flagSet.String("edge-ip-version", "", "") |
| 18 | + flagSet.String("protocol", "", "") |
| 19 | + flagSet.String("url", "", "") |
| 20 | + flagSet.String("quick-service", "", "") |
| 21 | + |
| 22 | + c := cli.NewContext(cli.NewApp(), flagSet, nil) |
| 23 | + _ = c.Set("edge-ip-version", "auto") |
| 24 | + _ = c.Set("protocol", "quic") |
| 25 | + _ = c.Set("url", "http://localhost:8080") |
| 26 | + _ = c.Set("quick-service", serverUrl) |
| 27 | + return c |
| 28 | +} |
| 29 | + |
| 30 | +// @noinspection SpellCheckingInspection |
| 31 | +func TestQuickTunnel(t *testing.T) { |
| 32 | + var tests = []struct { |
| 33 | + name string |
| 34 | + statusCode int |
| 35 | + response string |
| 36 | + wantErr bool |
| 37 | + expectedErr error |
| 38 | + }{ |
| 39 | + { |
| 40 | + name: "200 OK response from server, valid response", |
| 41 | + statusCode: http.StatusOK, |
| 42 | + response: `{"success":true,"result":{"id":"0347c3ea-504b-47bc-8e2c-339961e6ea3e","name":"qt-not-a-real-name","hostname":"not-a-real-hostname.trycloudflare.com","account_tag":"not-an-account-tag","secret":"notreallyasecret"},"errors":[]}`, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "200 OK response from server, bad tunnel ID", |
| 46 | + statusCode: http.StatusOK, |
| 47 | + response: `{"success":true,"result":{"id":"not-a-uuid","name":"qt-not-a-real-name","hostname":"not-a-real-hostname.trycloudflare.com","account_tag":"not-an-account-tag","secret":"notreallyasecret"},"errors":[]}`, |
| 48 | + wantErr: true, |
| 49 | + expectedErr: errors.New("failed to parse quick Tunnel ID: invalid UUID length: 10"), |
| 50 | + }, |
| 51 | + { |
| 52 | + name: "200 OK response from server, bad JSON", |
| 53 | + statusCode: http.StatusOK, |
| 54 | + response: `This is not JSON!`, |
| 55 | + wantErr: true, |
| 56 | + expectedErr: errors.New("failed to unmarshal quick Tunnel \"This is not JSON!\": invalid character 'T' looking for beginning of value"), |
| 57 | + }, |
| 58 | + { |
| 59 | + name: "429 Too Many Requests response from server", |
| 60 | + statusCode: http.StatusTooManyRequests, |
| 61 | + response: `error`, |
| 62 | + wantErr: true, |
| 63 | + expectedErr: errors.New("rate limit exceeded; wait a while and try again"), |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "400 Bad Request response from server", |
| 67 | + statusCode: http.StatusBadRequest, |
| 68 | + response: `error`, |
| 69 | + wantErr: true, |
| 70 | + expectedErr: errors.New("HTTP error 400"), |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + for i, tt := range tests { |
| 75 | + t.Run(tt.name, func(t *testing.T) { |
| 76 | + // Initialize tunnel subcommand |
| 77 | + bInfo := cliutil.GetBuildInfo("", "DEV") |
| 78 | + graceShutdownC := make(chan struct{}) |
| 79 | + Init(bInfo, graceShutdownC) |
| 80 | + |
| 81 | + // Create a test HTTP server to act in place of the Cloudflare service |
| 82 | + serverReceivedRequest := false |
| 83 | + server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) { |
| 84 | + // All requests should be to /tunnel |
| 85 | + assert.Equal(t, "/tunnel", req.URL.String()) |
| 86 | + |
| 87 | + serverReceivedRequest = true |
| 88 | + |
| 89 | + rw.WriteHeader(tt.statusCode) |
| 90 | + _, _ = rw.Write([]byte(tt.response)) |
| 91 | + })) |
| 92 | + defer server.Close() |
| 93 | + |
| 94 | + if !tt.wantErr { |
| 95 | + // Close the shutdown channel now so that the tunnel subcommand doesn't proceed to the quic negotiation |
| 96 | + close(graceShutdownC) |
| 97 | + } |
| 98 | + |
| 99 | + err := TunnelCommand(makeContext(i, server.URL)) |
| 100 | + |
| 101 | + if tt.wantErr { |
| 102 | + assert.Equal(t, tt.expectedErr.Error(), err.Error()) |
| 103 | + } else { |
| 104 | + assert.Nil(t, err) |
| 105 | + } |
| 106 | + assert.True(t, serverReceivedRequest) |
| 107 | + }) |
| 108 | + } |
| 109 | +} |
0 commit comments