Skip to content

Commit 9339653

Browse files
committed
Add playistID cli flag test
1 parent ac4a898 commit 9339653

File tree

2 files changed

+50
-18
lines changed

2 files changed

+50
-18
lines changed

internal/limiter/limiter.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
)
3030

3131
type LimitTransport struct {
32-
roundtrip http.RoundTripper
32+
transport http.RoundTripper
3333
limitRange LimitRange
3434
reader limitChecker
3535
readerInit bool
@@ -188,7 +188,7 @@ func NewLimitTransport(logger utils.Logger, rt http.RoundTripper, lr LimitRange,
188188

189189
lt := &LimitTransport{
190190
logger: logger,
191-
roundtrip: rt,
191+
transport: rt,
192192
limitRange: lr,
193193
filesize: filesize,
194194
rateLimit: ratelimit,
@@ -238,7 +238,7 @@ func (t *LimitTransport) RoundTrip(r *http.Request) (*http.Response, error) {
238238
}
239239
t.logger.Debugf("Requesting URL %q\n", r.URL)
240240

241-
resp, err := t.roundtrip.RoundTrip(r)
241+
resp, err := t.transport.RoundTrip(r)
242242
if err == nil {
243243
t.logger.Debugf("Response status code: %d\n", resp.StatusCode)
244244
if resp.Body != nil {

test/upload_test.go

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import (
2424
"net/http/httptest"
2525
"net/url"
2626
"os"
27+
"strings"
2728
"testing"
2829
"time"
2930

@@ -61,8 +62,10 @@ type mockReader struct {
6162
}
6263

6364
func (m *mockTransport) RoundTrip(r *http.Request) (*http.Response, error) {
64-
fmt.Printf("original request URL %s\n", r.URL.String())
65-
r.URL = m.url
65+
fmt.Printf("%s URL %s\n", r.Method, r.URL.String())
66+
r.URL.Scheme = m.url.Scheme
67+
r.URL.Host = m.url.Host
68+
6669
return http.DefaultTransport.RoundTrip(r)
6770
}
6871

@@ -87,8 +90,7 @@ func TestMain(m *testing.M) {
8790
testServer = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
8891

8992
// be sure to read the request body otherwise the client gets confused
90-
// body, err := io.ReadAll(r.Body)
91-
_, err := io.ReadAll(r.Body)
93+
_, err := io.Copy(io.Discard, r.Body)
9294
if err != nil {
9395
log.Printf("Error reading body: %v", err)
9496
http.Error(w, "can't read body", http.StatusBadRequest)
@@ -97,20 +99,50 @@ func TestMain(m *testing.M) {
9799
// log.Printf("Mock server: request body length %d", len(body))
98100

99101
w.Header().Set("Content-Type", "application/json")
100-
if r.Host == "oauth2.googleapis.com" {
102+
switch r.Host {
103+
case "oauth2.googleapis.com":
101104
fmt.Fprintln(w, oAuthResponse)
102-
} else if r.Host == "youtube.googleapis.com" {
103-
video := youtube.Video{
104-
Id: "test",
105-
}
106-
videoJ, err := json.Marshal(video)
107-
if err != nil {
108-
fmt.Printf("json marshall error %s\n", err)
109-
http.Error(w, err.Error(), http.StatusInternalServerError)
110-
return
105+
case "youtube.googleapis.com":
106+
107+
if strings.HasPrefix(r.URL.RequestURI(), "/upload") {
108+
video := youtube.Video{
109+
Id: "test",
110+
}
111+
videoJ, err := json.Marshal(video)
112+
if err != nil {
113+
fmt.Printf("json marshall error %s\n", err)
114+
http.Error(w, err.Error(), http.StatusInternalServerError)
115+
return
116+
}
117+
fmt.Fprintln(w, string(videoJ))
118+
} else if strings.HasPrefix(r.URL.RequestURI(), "/youtube/v3/playlists") {
119+
playlist1 := &youtube.Playlist{
120+
Id: "xxxx",
121+
Snippet: &youtube.PlaylistSnippet{
122+
Title: "Test Playlist 1",
123+
},
124+
}
125+
playlist2 := &youtube.Playlist{
126+
Id: "yyyy",
127+
Snippet: &youtube.PlaylistSnippet{
128+
Title: "Test Playlist 2",
129+
},
130+
}
131+
playlistResponse := youtube.PlaylistListResponse{
132+
Items: []*youtube.Playlist{playlist1, playlist2},
133+
}
134+
playlistJ, err := json.Marshal(playlistResponse)
135+
if err != nil {
136+
fmt.Printf("json marshall error %s\n", err)
137+
http.Error(w, err.Error(), http.StatusInternalServerError)
138+
return
139+
}
140+
fmt.Fprintln(w, string(playlistJ))
141+
} else if strings.HasPrefix(r.URL.RequestURI(), "/youtube/v3/playlistItems") {
142+
fmt.Fprintln(w, "{}")
111143
}
112-
fmt.Fprintln(w, string(videoJ))
113144
}
145+
114146
}))
115147
defer testServer.Close()
116148

0 commit comments

Comments
 (0)