cloudflare/cloudflared

Public

mirrored from https://github.com/cloudflare/cloudflaredAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
2025.11.1

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

cfapi/tunnel_test.go

102lines · modecode

1package cfapi
2
3import (
4 "bytes"
5 "fmt"
6 "net"
7 "reflect"
8 "strings"
9 "testing"
10 "time"
11
12 "github.com/google/uuid"
13 "github.com/stretchr/testify/assert"
14)
15
16var loc, _ = time.LoadLocation("UTC")
17
18func Test_unmarshalTunnel(t *testing.T) {
19 type args struct {
20 body string
21 }
22 tests := []struct {
23 name string
24 args args
25 want *Tunnel
26 wantErr bool
27 }{
28 {
29 name: "empty list",
30 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"}}}`},
31 want: &Tunnel{
32 ID: uuid.MustParse("b34cc7ce-925b-46ee-bc23-4cb5c18d8292"),
33 Name: "qt-bIWWN7D662ogh61pCPfu5s2XgqFY1OyV",
34 CreatedAt: time.Date(2021, 07, 29, 13, 46, 14, 90955000, loc),
35 DeletedAt: time.Date(2021, 07, 29, 14, 7, 27, 559047000, loc),
36 Connections: nil,
37 },
38 },
39 }
40 for _, tt := range tests {
41 t.Run(tt.name, func(t *testing.T) {
42 got, err := unmarshalTunnel(strings.NewReader(tt.args.body))
43 if (err != nil) != tt.wantErr {
44 t.Errorf("unmarshalTunnel() error = %v, wantErr %v", err, tt.wantErr)
45 return
46 }
47 if !reflect.DeepEqual(got, tt.want) {
48 t.Errorf("unmarshalTunnel() = %v, want %v", got, tt.want)
49 }
50 })
51 }
52}
53
54func TestUnmarshalTunnelOk(t *testing.T) {
55
56 jsonBody := `{"success": true, "result": {"id": "00000000-0000-0000-0000-000000000000","name":"test","created_at":"0001-01-01T00:00:00Z","connections":[]}}`
57 expected := Tunnel{
58 ID: uuid.Nil,
59 Name: "test",
60 CreatedAt: time.Time{},
61 Connections: []Connection{},
62 }
63 actual, err := unmarshalTunnel(bytes.NewReader([]byte(jsonBody)))
64 assert.NoError(t, err)
65 assert.Equal(t, &expected, actual)
66}
67
68func TestUnmarshalTunnelErr(t *testing.T) {
69
70 tests := []string{
71 `abc`,
72 `{"success": true, "result": abc}`,
73 `{"success": false, "result": {"id": "00000000-0000-0000-0000-000000000000","name":"test","created_at":"0001-01-01T00:00:00Z","connections":[]}}}`,
74 `{"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":[]}}}`,
75 }
76
77 for i, test := range tests {
78 _, err := unmarshalTunnel(bytes.NewReader([]byte(test)))
79 assert.Error(t, err, fmt.Sprintf("Test #%v failed", i))
80 }
81}
82
83func TestUnmarshalConnections(t *testing.T) {
84 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"}]}`
85 expected := ActiveClient{
86 ID: uuid.MustParse("d4041254-91e3-4deb-bd94-b46e11680b1e"),
87 Features: []string{"ha-origin"},
88 Version: "2021.2.5",
89 Arch: "darwin_amd64",
90 RunAt: time.Time{},
91 Connections: []Connection{{
92 ID: uuid.MustParse("ac2286e5-c708-4588-a6a0-ba6b51940019"),
93 ColoName: "LIS",
94 IsPendingReconnect: false,
95 OriginIP: net.ParseIP("148.38.28.2"),
96 OpenedAt: time.Time{},
97 }},
98 }
99 actual, err := parseConnectionsDetails(bytes.NewReader([]byte(jsonBody)))
100 assert.NoError(t, err)
101 assert.Equal(t, []*ActiveClient{&expected}, actual)
102}