cloudflare/cloudflared
Publicmirrored from https://github.com/cloudflare/cloudflaredAvailable
cfapi/tunnel_test.go
149lines · modecode
| 1 | package cfapi |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "io/ioutil" |
| 7 | "net" |
| 8 | "reflect" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/google/uuid" |
| 14 | "github.com/stretchr/testify/assert" |
| 15 | ) |
| 16 | |
| 17 | var loc, _ = time.LoadLocation("UTC") |
| 18 | |
| 19 | func Test_parseListTunnels(t *testing.T) { |
| 20 | type args struct { |
| 21 | body string |
| 22 | } |
| 23 | tests := []struct { |
| 24 | name string |
| 25 | args args |
| 26 | want []*Tunnel |
| 27 | wantErr bool |
| 28 | }{ |
| 29 | { |
| 30 | name: "empty list", |
| 31 | args: args{body: `{"success": true, "result": []}`}, |
| 32 | want: []*Tunnel{}, |
| 33 | }, |
| 34 | { |
| 35 | name: "success is false", |
| 36 | args: args{body: `{"success": false, "result": []}`}, |
| 37 | wantErr: true, |
| 38 | }, |
| 39 | { |
| 40 | name: "errors are present", |
| 41 | args: args{body: `{"errors": [{"code": 1003, "message":"An A, AAAA or CNAME record already exists with that host"}], "result": []}`}, |
| 42 | wantErr: true, |
| 43 | }, |
| 44 | { |
| 45 | name: "invalid response", |
| 46 | args: args{body: `abc`}, |
| 47 | wantErr: true, |
| 48 | }, |
| 49 | } |
| 50 | for _, tt := range tests { |
| 51 | t.Run(tt.name, func(t *testing.T) { |
| 52 | body := ioutil.NopCloser(bytes.NewReader([]byte(tt.args.body))) |
| 53 | got, err := parseListTunnels(body) |
| 54 | if (err != nil) != tt.wantErr { |
| 55 | t.Errorf("parseListTunnels() error = %v, wantErr %v", err, tt.wantErr) |
| 56 | return |
| 57 | } |
| 58 | if !reflect.DeepEqual(got, tt.want) { |
| 59 | t.Errorf("parseListTunnels() = %v, want %v", got, tt.want) |
| 60 | } |
| 61 | }) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func Test_unmarshalTunnel(t *testing.T) { |
| 66 | type args struct { |
| 67 | body string |
| 68 | } |
| 69 | tests := []struct { |
| 70 | name string |
| 71 | args args |
| 72 | want *Tunnel |
| 73 | wantErr bool |
| 74 | }{ |
| 75 | { |
| 76 | name: "empty list", |
| 77 | args: args{body: `{"success": true, "result": {"id":"b34cc7ce-925b-46ee-bc23-4cb5c18d8292","created_at":"2021-07-29T13:46:14.090955Z","deleted_at":"2021-07-29T14:07:27.559047Z","name":"qt-bIWWN7D662ogh61pCPfu5s2XgqFY1OyV","account_id":6946212,"account_tag":"5ab4e9dfbd435d24068829fda0077963","conns_active_at":null,"conns_inactive_at":"2021-07-29T13:47:22.548482Z","tun_type":"cfd_tunnel","metadata":{"qtid":"a6fJROgkXutNruBGaJjD"}}}`}, |
| 78 | want: &Tunnel{ |
| 79 | ID: uuid.MustParse("b34cc7ce-925b-46ee-bc23-4cb5c18d8292"), |
| 80 | Name: "qt-bIWWN7D662ogh61pCPfu5s2XgqFY1OyV", |
| 81 | CreatedAt: time.Date(2021, 07, 29, 13, 46, 14, 90955000, loc), |
| 82 | DeletedAt: time.Date(2021, 07, 29, 14, 7, 27, 559047000, loc), |
| 83 | Connections: nil, |
| 84 | }, |
| 85 | }, |
| 86 | } |
| 87 | for _, tt := range tests { |
| 88 | t.Run(tt.name, func(t *testing.T) { |
| 89 | got, err := unmarshalTunnel(strings.NewReader(tt.args.body)) |
| 90 | if (err != nil) != tt.wantErr { |
| 91 | t.Errorf("unmarshalTunnel() error = %v, wantErr %v", err, tt.wantErr) |
| 92 | return |
| 93 | } |
| 94 | if !reflect.DeepEqual(got, tt.want) { |
| 95 | t.Errorf("unmarshalTunnel() = %v, want %v", got, tt.want) |
| 96 | } |
| 97 | }) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | func TestUnmarshalTunnelOk(t *testing.T) { |
| 102 | |
| 103 | jsonBody := `{"success": true, "result": {"id": "00000000-0000-0000-0000-000000000000","name":"test","created_at":"0001-01-01T00:00:00Z","connections":[]}}` |
| 104 | expected := Tunnel{ |
| 105 | ID: uuid.Nil, |
| 106 | Name: "test", |
| 107 | CreatedAt: time.Time{}, |
| 108 | Connections: []Connection{}, |
| 109 | } |
| 110 | actual, err := unmarshalTunnel(bytes.NewReader([]byte(jsonBody))) |
| 111 | assert.NoError(t, err) |
| 112 | assert.Equal(t, &expected, actual) |
| 113 | } |
| 114 | |
| 115 | func TestUnmarshalTunnelErr(t *testing.T) { |
| 116 | |
| 117 | tests := []string{ |
| 118 | `abc`, |
| 119 | `{"success": true, "result": abc}`, |
| 120 | `{"success": false, "result": {"id": "00000000-0000-0000-0000-000000000000","name":"test","created_at":"0001-01-01T00:00:00Z","connections":[]}}}`, |
| 121 | `{"errors": [{"code": 1003, "message":"An A, AAAA or CNAME record already exists with that host"}], "result": {"id": "00000000-0000-0000-0000-000000000000","name":"test","created_at":"0001-01-01T00:00:00Z","connections":[]}}}`, |
| 122 | } |
| 123 | |
| 124 | for i, test := range tests { |
| 125 | _, err := unmarshalTunnel(bytes.NewReader([]byte(test))) |
| 126 | assert.Error(t, err, fmt.Sprintf("Test #%v failed", i)) |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | func TestUnmarshalConnections(t *testing.T) { |
| 131 | jsonBody := `{"success":true,"messages":[],"errors":[],"result":[{"id":"d4041254-91e3-4deb-bd94-b46e11680b1e","features":["ha-origin"],"version":"2021.2.5","arch":"darwin_amd64","conns":[{"colo_name":"LIS","id":"ac2286e5-c708-4588-a6a0-ba6b51940019","is_pending_reconnect":false,"origin_ip":"148.38.28.2","opened_at":"0001-01-01T00:00:00Z"}],"run_at":"0001-01-01T00:00:00Z"}]}` |
| 132 | expected := ActiveClient{ |
| 133 | ID: uuid.MustParse("d4041254-91e3-4deb-bd94-b46e11680b1e"), |
| 134 | Features: []string{"ha-origin"}, |
| 135 | Version: "2021.2.5", |
| 136 | Arch: "darwin_amd64", |
| 137 | RunAt: time.Time{}, |
| 138 | Connections: []Connection{{ |
| 139 | ID: uuid.MustParse("ac2286e5-c708-4588-a6a0-ba6b51940019"), |
| 140 | ColoName: "LIS", |
| 141 | IsPendingReconnect: false, |
| 142 | OriginIP: net.ParseIP("148.38.28.2"), |
| 143 | OpenedAt: time.Time{}, |
| 144 | }}, |
| 145 | } |
| 146 | actual, err := parseConnectionsDetails(bytes.NewReader([]byte(jsonBody))) |
| 147 | assert.NoError(t, err) |
| 148 | assert.Equal(t, []*ActiveClient{&expected}, actual) |
| 149 | } |
| 150 | |