@@ -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
6364func (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