-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathconnection_get_resolve_test.go
More file actions
98 lines (83 loc) · 2.8 KB
/
connection_get_resolve_test.go
File metadata and controls
98 lines (83 loc) · 2.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package cmd
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/hookdeck/hookdeck-cli/pkg/hookdeck"
)
func TestResolveConnectionID_ByIDPrefix(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
mux.HandleFunc(hookdeck.APIPathPrefix+"/connections/web_resolve1", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method", http.StatusMethodNotAllowed)
return
}
_ = json.NewEncoder(w).Encode(map[string]any{"id": "web_resolve1", "team_id": "tm_1"})
})
srv := httptest.NewServer(mux)
t.Cleanup(srv.Close)
baseURL, err := url.Parse(srv.URL)
require.NoError(t, err)
client := &hookdeck.Client{BaseURL: baseURL, APIKey: "k"}
id, err := resolveConnectionID(context.Background(), client, "web_resolve1")
require.NoError(t, err)
assert.Equal(t, "web_resolve1", id)
}
func TestResolveConnectionID_ByName(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
mux.HandleFunc(hookdeck.APIPathPrefix+"/connections", func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
http.Error(w, "method", http.StatusMethodNotAllowed)
return
}
assert.Equal(t, "my-conn", r.URL.Query().Get("name"))
resp := map[string]any{
"models": []map[string]any{{"id": "web_named", "team_id": "tm_1"}},
"pagination": map[string]any{
"limit": float64(100),
},
}
_ = json.NewEncoder(w).Encode(resp)
})
srv := httptest.NewServer(mux)
t.Cleanup(srv.Close)
baseURL, err := url.Parse(srv.URL)
require.NoError(t, err)
client := &hookdeck.Client{BaseURL: baseURL, APIKey: "k"}
id, err := resolveConnectionID(context.Background(), client, "my-conn")
require.NoError(t, err)
assert.Equal(t, "web_named", id)
}
func TestResolveConnectionID_WebPrefix404FallsBackToNameLookup(t *testing.T) {
t.Parallel()
mux := http.NewServeMux()
mux.HandleFunc(hookdeck.APIPathPrefix+"/connections/web_stale", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNotFound)
_ = json.NewEncoder(w).Encode(map[string]any{"message": "not found"})
})
mux.HandleFunc(hookdeck.APIPathPrefix+"/connections", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, "web_stale", r.URL.Query().Get("name"))
resp := map[string]any{
"models": []map[string]any{{"id": "web_real", "team_id": "tm_1"}},
"pagination": map[string]any{
"limit": float64(100),
},
}
_ = json.NewEncoder(w).Encode(resp)
})
srv := httptest.NewServer(mux)
t.Cleanup(srv.Close)
baseURL, err := url.Parse(srv.URL)
require.NoError(t, err)
client := &hookdeck.Client{BaseURL: baseURL, APIKey: "k"}
id, err := resolveConnectionID(context.Background(), client, "web_stale")
require.NoError(t, err)
assert.Equal(t, "web_real", id)
}