-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitlab_test.go
More file actions
80 lines (66 loc) · 2.6 KB
/
Copy pathgitlab_test.go
File metadata and controls
80 lines (66 loc) · 2.6 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
package gitlab_test
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
"github.com/stretchr/testify/require"
"github.com/till/golangoss-bluesky/internal/gitlab"
)
const samplePage = `[
{"id":1,"name":"alpha","path_with_namespace":"a/alpha","web_url":"https://gitlab.com/a/alpha","description":"first","star_count":42,"topics":["go","cli"]},
{"id":2,"name":"beta","path_with_namespace":"b/beta","web_url":"https://gitlab.com/b/beta","description":"second","star_count":3,"topics":[]}
]`
func TestSearch_PassesFiltersAndMapsFields(t *testing.T) {
var gotQuery url.Values
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
require.Equal(t, "/api/v4/projects", r.URL.Path)
gotQuery = r.URL.Query()
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(samplePage))
}))
t.Cleanup(srv.Close)
c, err := gitlab.NewWithBaseURL("", srv.URL+"/api/v4", srv.Client())
require.NoError(t, err)
got, err := c.Search(t.Context(), gitlab.SearchOptions{
Language: "Go",
Query: "bot",
PerPage: 25,
Page: 1,
})
require.NoError(t, err)
require.Equal(t, "Go", gotQuery.Get("with_programming_language"))
require.Equal(t, "bot", gotQuery.Get("search"))
require.Equal(t, "public", gotQuery.Get("visibility"))
require.Equal(t, "star_count", gotQuery.Get("order_by"))
require.Equal(t, "desc", gotQuery.Get("sort"))
require.Equal(t, "25", gotQuery.Get("per_page"))
require.Len(t, got, 2)
require.Equal(t, "a/alpha", got[0].PathWithNS)
require.Equal(t, int64(42), got[0].Stars)
require.Equal(t, []string{"go", "cli"}, got[0].Topics)
require.Equal(t, "https://gitlab.com/b/beta", got[1].WebURL)
}
func TestSearch_MinStarsFiltersClientSide(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(samplePage))
}))
t.Cleanup(srv.Close)
c, err := gitlab.NewWithBaseURL("", srv.URL+"/api/v4", srv.Client())
require.NoError(t, err)
got, err := c.Search(t.Context(), gitlab.SearchOptions{MinStars: 10})
require.NoError(t, err)
require.Len(t, got, 1)
require.Equal(t, "a/alpha", got[0].PathWithNS)
}
func TestSearch_PropagatesError(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
http.Error(w, `{"message":"boom"}`, http.StatusInternalServerError)
}))
t.Cleanup(srv.Close)
c, err := gitlab.NewWithBaseURL("", srv.URL+"/api/v4", srv.Client())
require.NoError(t, err)
_, err = c.Search(t.Context(), gitlab.SearchOptions{Language: "Go"})
require.Error(t, err)
}