♻️ Migrate encoding/json to encoding/json/v2 and jsontext

This commit is contained in:
OpenWaygate committed 2026-08-29 19:01:26 +08:00
1 parent 2d27a90036
commit 2d3ed8e0ba
70 files changed
+381 -275

No files matched your search

+3 -3
View File
@@ -5,7 +5,7 @@ package abuseReport
import (
"bytes"
"encoding/json"
"encoding/json/v2"
"net/http"
"reflect"
"testing"
@@ -139,7 +139,7 @@ func TestAbuseReport_Insert(t *testing.T) {
TypeId string `json:"typeId"`
} `json:"subject"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
if err := json.UnmarshalRead(r.Body, &body); err != nil {
t.Fatalf("failed to decode request body: %v", err)
}
@@ -176,7 +176,7 @@ func TestAbuseReport_Insert(t *testing.T) {
} `json:"entity"`
} `json:"relatedEntities"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
if err := json.UnmarshalRead(r.Body, &body); err != nil {
t.Fatalf("failed to decode request body: %v", err)
}
+2 -2
View File
@@ -5,7 +5,7 @@ package auth
import (
"context"
"encoding/json"
"encoding/json/v2"
"fmt"
"log/slog"
"net"
@@ -264,7 +264,7 @@ func (s *svc) saveToken(token *oauth2.Token) error {
defer func() {
_ = f.Close()
}()
err = json.NewEncoder(f).Encode(token)
err = json.MarshalWrite(f, token)
if err != nil {
return fmt.Errorf("%s: %w", cacheTokenFailed, err)
}
+8 -3
View File
@@ -5,7 +5,7 @@ package auth
import (
"bytes"
"encoding/json"
"encoding/json/v2"
"fmt"
"net"
"net/http"
@@ -357,11 +357,16 @@ func TestGetCodeFromPrompt_ReadError(t *testing.T) {
s := NewY2BService(WithIO(errReader, &out)).(*svc)
_, err := s.getCodeFromPrompt("http://example.com/auth", "http://localhost:8216")
_, err := s.getCodeFromPrompt(
"http://example.com/auth", "http://localhost:8216",
)
if err == nil {
t.Fatalf("expected error from getCodeFromPrompt, got nil")
}
if !strings.Contains(err.Error(), "failed to read prompt") {
t.Errorf("expected error to contain %q, got %q", "failed to read prompt", err.Error())
t.Errorf(
"expected error to contain %q, got %q", "failed to read prompt",
err.Error(),
)
}
}
+2 -2
View File
@@ -5,7 +5,7 @@ package auth
import (
"context"
"encoding/json"
"encoding/json/v2"
"fmt"
"io"
"net/http"
@@ -48,7 +48,7 @@ func NewGoogleTokenVerifier(tokenInfoURL string) sdkauth.TokenVerifier {
Sub string `json:"sub"`
Error string `json:"error"`
}
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
if err := json.UnmarshalRead(resp.Body, &result); err != nil {
return nil, fmt.Errorf("%w: %w", sdkauth.ErrInvalidToken, err)
}
if result.Error != "" {
+35 -21
View File
@@ -4,7 +4,7 @@
package auth
import (
"encoding/json"
"encoding/json/v2"
"errors"
"net/http"
"net/http/httptest"
@@ -14,19 +14,25 @@ import (
)
func TestGoogleTokenVerifier_ValidToken(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
token := r.URL.Query().Get("access_token")
if token != "valid-google-token" {
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(map[string]string{"error": "invalid_token"})
return
}
_ = json.NewEncoder(w).Encode(map[string]any{
"expires_in": "3600",
"scope": "https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtube.force-ssl https://www.googleapis.com/auth/youtube.channel-memberships.creator",
"sub": "user-123",
})
}))
ts := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
token := r.URL.Query().Get("access_token")
if token != "valid-google-token" {
w.WriteHeader(http.StatusBadRequest)
_ = json.MarshalWrite(w, map[string]string{"error": "invalid_token"})
return
}
_ = json.MarshalWrite(
w, map[string]any{
"expires_in": "3600",
"scope": "https://www.googleapis.com/auth/youtube https://www.googleapis.com/auth/youtube.force-ssl https://www.googleapis.com/auth/youtube.channel-memberships.creator",
"sub": "user-123",
},
)
},
),
)
defer ts.Close()
verifier := NewGoogleTokenVerifier(ts.URL)
@@ -50,10 +56,14 @@ func TestGoogleTokenVerifier_ValidToken(t *testing.T) {
}
func TestGoogleTokenVerifier_InvalidToken(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
_ = json.NewEncoder(w).Encode(map[string]string{"error": "invalid_token"})
}))
ts := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
_ = json.MarshalWrite(w, map[string]string{"error": "invalid_token"})
},
),
)
defer ts.Close()
verifier := NewGoogleTokenVerifier(ts.URL)
@@ -67,9 +77,13 @@ func TestGoogleTokenVerifier_InvalidToken(t *testing.T) {
}
func TestGoogleTokenVerifier_ServerError(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
ts := httptest.NewServer(
http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
},
),
)
defer ts.Close()
verifier := NewGoogleTokenVerifier(ts.URL)
+3 -3
View File
@@ -5,7 +5,7 @@ package liveBroadcast
import (
"bytes"
"encoding/json"
"encoding/json/v2"
"io"
"math"
"net/http"
@@ -364,7 +364,7 @@ func TestLiveBroadcast_Insert(t *testing.T) {
PrivacyStatus string `json:"privacyStatus"`
} `json:"status"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
if err := json.UnmarshalRead(r.Body, &body); err != nil {
t.Fatalf("failed to decode request body: %v", err)
}
@@ -676,7 +676,7 @@ func TestLiveBroadcast_InsertCuepoint(t *testing.T) {
CueType string `json:"cueType"`
DurationSecs int64 `json:"durationSecs"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
if err := json.UnmarshalRead(r.Body, &body); err != nil {
t.Fatalf("failed to decode request body: %v", err)
}
if body.CueType != "cueTypeAd" {
+3 -3
View File
@@ -5,7 +5,7 @@ package liveChatBan
import (
"bytes"
"encoding/json"
"encoding/json/v2"
"net/http"
"reflect"
"testing"
@@ -151,7 +151,7 @@ func TestLiveChatBan_Insert(t *testing.T) {
Type string `json:"type"`
} `json:"snippet"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
if err := json.UnmarshalRead(r.Body, &body); err != nil {
t.Fatalf("failed to decode request body: %v", err)
}
@@ -190,7 +190,7 @@ func TestLiveChatBan_Insert(t *testing.T) {
Type string `json:"type"`
} `json:"snippet"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
if err := json.UnmarshalRead(r.Body, &body); err != nil {
t.Fatalf("failed to decode request body: %v", err)
}
+2 -2
View File
@@ -5,7 +5,7 @@ package liveChatMessage
import (
"bytes"
"encoding/json"
"encoding/json/v2"
"io"
"math"
"net/http"
@@ -290,7 +290,7 @@ func TestLiveChatMessage_Insert(t *testing.T) {
Type string `json:"type"`
} `json:"snippet"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
if err := json.UnmarshalRead(r.Body, &body); err != nil {
t.Fatalf("failed to decode request body: %v", err)
}
@@ -5,7 +5,7 @@ package liveChatModerator
import (
"bytes"
"encoding/json"
"encoding/json/v2"
"io"
"math"
"net/http"
@@ -117,7 +117,9 @@ func TestNewLiveChatModerator(t *testing.T) {
if got := NewLiveChatModerator(tt.args.opts...); !reflect.DeepEqual(
got, tt.want,
) {
t.Errorf("%s\nNewLiveChatModerator() = %v\nwant %v", tt.name, got, tt.want)
t.Errorf(
"%s\nNewLiveChatModerator() = %v\nwant %v", tt.name, got, tt.want,
)
}
},
)
@@ -186,11 +188,16 @@ func TestLiveChatModerator_Get(t *testing.T) {
m := NewLiveChatModerator(opts...)
got, err := m.Get()
if (err != nil) != tt.wantErr {
t.Errorf("LiveChatModerator.Get() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf(
"LiveChatModerator.Get() error = %v, wantErr %v", err, tt.wantErr,
)
return
}
if len(got) != tt.wantLen {
t.Errorf("LiveChatModerator.Get() got length = %v, want %v", len(got), tt.wantLen)
t.Errorf(
"LiveChatModerator.Get() got length = %v, want %v", len(got),
tt.wantLen,
)
}
},
)
@@ -272,7 +279,7 @@ func TestLiveChatModerator_Insert(t *testing.T) {
} `json:"moderatorDetails"`
} `json:"snippet"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
if err := json.UnmarshalRead(r.Body, &body); err != nil {
t.Fatalf("failed to decode request body: %v", err)
}
+2 -2
View File
@@ -5,7 +5,7 @@ package liveStream
import (
"bytes"
"encoding/json"
"encoding/json/v2"
"io"
"math"
"net/http"
@@ -324,7 +324,7 @@ func TestLiveStream_Insert(t *testing.T) {
Resolution string `json:"resolution"`
} `json:"cdn"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
if err := json.UnmarshalRead(r.Body, &body); err != nil {
t.Fatalf("failed to decode request body: %v", err)
}
+7 -4
View File
@@ -5,7 +5,7 @@ package playlist
import (
"bytes"
"encoding/json"
"encoding/json/v2"
"io"
"math"
"net/http"
@@ -332,7 +332,8 @@ func TestPlaylist_Get_Pagination(t *testing.T) {
}
func TestPlaylist_List(t *testing.T) {
common.RunListTest(t, `{
common.RunListTest(
t, `{
"items": [
{
"id": "playlist-1",
@@ -344,7 +345,9 @@ func TestPlaylist_List(t *testing.T) {
]
}`,
func(svc *youtube.Service, output string) func(io.Writer) error {
p := NewPlaylist(WithService(svc), WithOutput(output), WithIds([]string{"playlist-1"}))
p := NewPlaylist(
WithService(svc), WithOutput(output), WithIds([]string{"playlist-1"}),
)
return p.List
},
)
@@ -459,7 +462,7 @@ func TestPlaylist_Update(t *testing.T) {
}
var playlist youtube.Playlist
if err := json.NewDecoder(r.Body).Decode(&playlist); err != nil {
if err := json.UnmarshalRead(r.Body, &playlist); err != nil {
t.Errorf("failed to decode body: %v", err)
}
if playlist.Snippet.Title != "Updated Title" {
+7 -4
View File
@@ -5,7 +5,7 @@ package subscription
import (
"bytes"
"encoding/json"
"encoding/json/v2"
"io"
"math"
"net/http"
@@ -331,7 +331,8 @@ func TestSubscription_Get_Pagination(t *testing.T) {
}
func TestSubscription_List(t *testing.T) {
common.RunListTest(t, `{
common.RunListTest(
t, `{
"items": [
{
"id": "sub-1",
@@ -346,7 +347,9 @@ func TestSubscription_List(t *testing.T) {
]
}`,
func(svc *youtube.Service, output string) func(io.Writer) error {
s := NewSubscription(WithService(svc), WithOutput(output), WithIds([]string{"sub-1"}))
s := NewSubscription(
WithService(svc), WithOutput(output), WithIds([]string{"sub-1"}),
)
return s.List
},
)
@@ -385,7 +388,7 @@ func TestSubscription_Insert(t *testing.T) {
ChannelId string `json:"channelId"`
} `json:"snippet"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
if err := json.UnmarshalRead(r.Body, &body); err != nil {
t.Fatalf("failed to decode request body: %v", err)
}
+54 -20
View File
@@ -5,7 +5,7 @@ package thirdPartyLink
import (
"bytes"
"encoding/json"
"encoding/json/v2"
"io"
"net/http"
"reflect"
@@ -79,7 +79,9 @@ func TestNewThirdPartyLink(t *testing.T) {
if got := NewThirdPartyLink(tt.args.opts...); !reflect.DeepEqual(
got, tt.want,
) {
t.Errorf("%s\nNewThirdPartyLink() = %v\nwant %v", tt.name, got, tt.want)
t.Errorf(
"%s\nNewThirdPartyLink() = %v\nwant %v", tt.name, got, tt.want,
)
}
},
)
@@ -102,7 +104,10 @@ func TestThirdPartyLink_Get(t *testing.T) {
},
verify: func(r *http.Request) {
if r.URL.Query().Get("linkingToken") != "token123" {
t.Errorf("expected linkingToken=token123, got %s", r.URL.Query().Get("linkingToken"))
t.Errorf(
"expected linkingToken=token123, got %s",
r.URL.Query().Get("linkingToken"),
)
}
},
wantLen: 1,
@@ -116,7 +121,9 @@ func TestThirdPartyLink_Get(t *testing.T) {
},
verify: func(r *http.Request) {
if r.URL.Query().Get("type") != "channelToStoreLink" {
t.Errorf("expected type=channelToStoreLink, got %s", r.URL.Query().Get("type"))
t.Errorf(
"expected type=channelToStoreLink, got %s", r.URL.Query().Get("type"),
)
}
},
wantLen: 1,
@@ -130,7 +137,10 @@ func TestThirdPartyLink_Get(t *testing.T) {
},
verify: func(r *http.Request) {
if r.URL.Query().Get("externalChannelId") != "ext-channel-123" {
t.Errorf("expected externalChannelId=ext-channel-123, got %s", r.URL.Query().Get("externalChannelId"))
t.Errorf(
"expected externalChannelId=ext-channel-123, got %s",
r.URL.Query().Get("externalChannelId"),
)
}
},
wantLen: 1,
@@ -163,11 +173,15 @@ func TestThirdPartyLink_Get(t *testing.T) {
tpl := NewThirdPartyLink(opts...)
got, err := tpl.Get()
if (err != nil) != tt.wantErr {
t.Errorf("ThirdPartyLink.Get() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf(
"ThirdPartyLink.Get() error = %v, wantErr %v", err, tt.wantErr,
)
return
}
if len(got) != tt.wantLen {
t.Errorf("ThirdPartyLink.Get() got length = %v, want %v", len(got), tt.wantLen)
t.Errorf(
"ThirdPartyLink.Get() got length = %v, want %v", len(got), tt.wantLen,
)
}
},
)
@@ -175,7 +189,8 @@ func TestThirdPartyLink_Get(t *testing.T) {
}
func TestThirdPartyLink_List(t *testing.T) {
common.RunListTest(t, `{
common.RunListTest(
t, `{
"items": [
{
"linkingToken": "token123",
@@ -225,7 +240,7 @@ func TestThirdPartyLink_Insert(t *testing.T) {
LinkStatus string `json:"linkStatus"`
} `json:"status"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
if err := json.UnmarshalRead(r.Body, &body); err != nil {
t.Fatalf("failed to decode request body: %v", err)
}
@@ -233,10 +248,14 @@ func TestThirdPartyLink_Insert(t *testing.T) {
t.Errorf("expected linkingToken=token123, got %s", body.LinkingToken)
}
if body.Snippet.Type != "channelToStoreLink" {
t.Errorf("expected snippet.type=channelToStoreLink, got %s", body.Snippet.Type)
t.Errorf(
"expected snippet.type=channelToStoreLink, got %s", body.Snippet.Type,
)
}
if body.Status.LinkStatus != "pending" {
t.Errorf("expected status.linkStatus=pending, got %s", body.Status.LinkStatus)
t.Errorf(
"expected status.linkStatus=pending, got %s", body.Status.LinkStatus,
)
}
},
wantErr: false,
@@ -262,7 +281,9 @@ func TestThirdPartyLink_Insert(t *testing.T) {
tpl := NewThirdPartyLink(opts...)
var buf bytes.Buffer
if err := tpl.Insert(&buf); (err != nil) != tt.wantErr {
t.Errorf("ThirdPartyLink.Insert() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf(
"ThirdPartyLink.Insert() error = %v, wantErr %v", err, tt.wantErr,
)
}
},
)
@@ -296,7 +317,7 @@ func TestThirdPartyLink_Update(t *testing.T) {
LinkStatus string `json:"linkStatus"`
} `json:"status"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
if err := json.UnmarshalRead(r.Body, &body); err != nil {
t.Fatalf("failed to decode request body: %v", err)
}
@@ -304,7 +325,9 @@ func TestThirdPartyLink_Update(t *testing.T) {
t.Errorf("expected linkingToken=token123, got %s", body.LinkingToken)
}
if body.Status.LinkStatus != "linked" {
t.Errorf("expected status.linkStatus=linked, got %s", body.Status.LinkStatus)
t.Errorf(
"expected status.linkStatus=linked, got %s", body.Status.LinkStatus,
)
}
},
wantErr: false,
@@ -319,11 +342,13 @@ func TestThirdPartyLink_Update(t *testing.T) {
func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
if r.Method == "GET" {
_, _ = w.Write([]byte(`{
_, _ = w.Write(
[]byte(`{
"items": [
{"linkingToken": "token123", "snippet": {"type": "channelToStoreLink"}, "status": {"linkStatus": "pending"}}
]
}`))
}`),
)
return
}
if tt.verify != nil {
@@ -338,7 +363,9 @@ func TestThirdPartyLink_Update(t *testing.T) {
tpl := NewThirdPartyLink(opts...)
var buf bytes.Buffer
if err := tpl.Update(&buf); (err != nil) != tt.wantErr {
t.Errorf("ThirdPartyLink.Update() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf(
"ThirdPartyLink.Update() error = %v, wantErr %v", err, tt.wantErr,
)
}
},
)
@@ -363,10 +390,15 @@ func TestThirdPartyLink_Delete(t *testing.T) {
t.Errorf("expected DELETE, got %s", r.Method)
}
if r.URL.Query().Get("linkingToken") != "token123" {
t.Errorf("expected linkingToken=token123, got %s", r.URL.Query().Get("linkingToken"))
t.Errorf(
"expected linkingToken=token123, got %s",
r.URL.Query().Get("linkingToken"),
)
}
if r.URL.Query().Get("type") != "channelToStoreLink" {
t.Errorf("expected type=channelToStoreLink, got %s", r.URL.Query().Get("type"))
t.Errorf(
"expected type=channelToStoreLink, got %s", r.URL.Query().Get("type"),
)
}
},
wantErr: false,
@@ -391,7 +423,9 @@ func TestThirdPartyLink_Delete(t *testing.T) {
tpl := NewThirdPartyLink(opts...)
var buf bytes.Buffer
if err := tpl.Delete(&buf); (err != nil) != tt.wantErr {
t.Errorf("ThirdPartyLink.Delete() error = %v, wantErr %v", err, tt.wantErr)
t.Errorf(
"ThirdPartyLink.Delete() error = %v, wantErr %v", err, tt.wantErr,
)
}
},
)
+4 -3
View File
@@ -7,7 +7,8 @@ import (
"bufio"
"crypto/rand"
"encoding/base64"
"encoding/json"
"encoding/json/jsontext"
"encoding/json/v2"
"errors"
"fmt"
"io"
@@ -39,7 +40,7 @@ var IsInteractive = func(v any) bool {
func PrintJSON(data any, writer io.Writer) {
var marshalled []byte
if IsInteractive(writer) {
marshalled, _ = json.MarshalIndent(data, "", " ")
marshalled, _ = json.Marshal(data, jsontext.WithIndent(" "))
} else {
marshalled, _ = json.Marshal(data)
}
@@ -81,7 +82,7 @@ func GetFileName(file string) string {
}
func IsJson(s string) bool {
var js json.RawMessage
var js jsontext.Value
return json.Unmarshal([]byte(s), &js) == nil
}
+60 -21
View File
@@ -5,7 +5,7 @@ package video
import (
"bytes"
"encoding/json"
"encoding/json/v2"
"io"
"math"
"mime"
@@ -546,7 +546,7 @@ func TestVideo_Insert(t *testing.T) {
defer func() { _ = part.Close() }()
var body youtube.Video
if err := json.NewDecoder(part).Decode(&body); err != nil {
if err := json.UnmarshalRead(part, &body); err != nil {
t.Fatalf("failed to decode video from request body: %v", err)
}
return &body
@@ -650,11 +650,17 @@ func TestVideo_Insert(t *testing.T) {
t.Errorf("expected POST, got %s", r.Method)
}
if !strings.Contains(r.URL.Query().Get("part"), "recordingDetails") {
t.Errorf("expected part to contain recordingDetails, got %s", r.URL.Query().Get("part"))
t.Errorf(
"expected part to contain recordingDetails, got %s",
r.URL.Query().Get("part"),
)
}
body := decodeMultipartVideo(t, r)
if body.RecordingDetails == nil || body.RecordingDetails.RecordingDate != "2024-06-15T10:00:00Z" {
t.Errorf("expected recordingDetails.recordingDate=2024-06-15T10:00:00Z, got %+v", body.RecordingDetails)
t.Errorf(
"expected recordingDetails.recordingDate=2024-06-15T10:00:00Z, got %+v",
body.RecordingDetails,
)
}
if body.Status == nil || !body.Status.SelfDeclaredMadeForKids {
t.Errorf("expected status.selfDeclaredMadeForKids=true")
@@ -683,7 +689,10 @@ func TestVideo_Insert(t *testing.T) {
}
body := decodeMultipartVideo(t, r)
if body.Snippet == nil || body.Snippet.Title != "test_video" {
t.Errorf("expected snippet.title=test_video (from filename), got %+v", body.Snippet)
t.Errorf(
"expected snippet.title=test_video (from filename), got %+v",
body.Snippet,
)
}
},
wantErr: false,
@@ -708,7 +717,10 @@ func TestVideo_Insert(t *testing.T) {
}
}
if count != 1 {
t.Errorf("expected exactly 1 yutu🐰 tag, got %d in %v", count, body.Snippet.Tags)
t.Errorf(
"expected exactly 1 yutu🐰 tag, got %d in %v", count,
body.Snippet.Tags,
)
}
},
wantErr: false,
@@ -725,7 +737,9 @@ func TestVideo_Insert(t *testing.T) {
defer func() { pkg.Root = oldRoot }()
defer func() { _ = root.Close() }()
err = os.WriteFile(tmpDir+"/test_video.mp4", []byte("dummy video content"), 0644)
err = os.WriteFile(
tmpDir+"/test_video.mp4", []byte("dummy video content"), 0644,
)
if err != nil {
t.Fatalf("failed to create dummy file: %v", err)
}
@@ -814,7 +828,7 @@ func TestVideo_Update(t *testing.T) {
}
var body youtube.Video
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
if err := json.UnmarshalRead(r.Body, &body); err != nil {
t.Fatalf("failed to decode update body: %v", err)
}
if body.Id != "video-id" {
@@ -871,20 +885,27 @@ func TestVideo_Update(t *testing.T) {
verify: func(r *http.Request) {
if r.Method == "PUT" {
var body youtube.Video
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
if err := json.UnmarshalRead(r.Body, &body); err != nil {
t.Fatalf("failed to decode update body: %v", err)
}
if body.Snippet.DefaultLanguage != "ja" {
t.Errorf("expected defaultLanguage=ja, got %s", body.Snippet.DefaultLanguage)
t.Errorf(
"expected defaultLanguage=ja, got %s", body.Snippet.DefaultLanguage,
)
}
if body.Status.License != "creativeCommon" {
t.Errorf("expected license=creativeCommon, got %s", body.Status.License)
t.Errorf(
"expected license=creativeCommon, got %s", body.Status.License,
)
}
if body.Snippet.CategoryId != "22" {
t.Errorf("expected categoryId=22, got %s", body.Snippet.CategoryId)
}
if body.Status.PrivacyStatus != "unlisted" {
t.Errorf("expected privacyStatus=unlisted, got %s", body.Status.PrivacyStatus)
t.Errorf(
"expected privacyStatus=unlisted, got %s",
body.Status.PrivacyStatus,
)
}
found := false
for _, tag := range body.Snippet.Tags {
@@ -910,14 +931,20 @@ func TestVideo_Update(t *testing.T) {
verify: func(r *http.Request) {
if r.Method == "PUT" {
var body youtube.Video
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
if err := json.UnmarshalRead(r.Body, &body); err != nil {
t.Fatalf("failed to decode update body: %v", err)
}
if body.Status.PublishAt != "2026-08-18T15:00:00Z" {
t.Errorf("expected publishAt=2026-08-18T15:00:00Z, got %s", body.Status.PublishAt)
t.Errorf(
"expected publishAt=2026-08-18T15:00:00Z, got %s",
body.Status.PublishAt,
)
}
if body.Status.PrivacyStatus != "private" {
t.Errorf("expected privacyStatus=private when scheduling, got %s", body.Status.PrivacyStatus)
t.Errorf(
"expected privacyStatus=private when scheduling, got %s",
body.Status.PrivacyStatus,
)
}
}
},
@@ -935,14 +962,20 @@ func TestVideo_Update(t *testing.T) {
verify: func(r *http.Request) {
if r.Method == "PUT" {
var body youtube.Video
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
if err := json.UnmarshalRead(r.Body, &body); err != nil {
t.Fatalf("failed to decode update body: %v", err)
}
if body.Status.PublishAt != "2026-08-18T15:00:00Z" {
t.Errorf("expected publishAt=2026-08-18T15:00:00Z, got %s", body.Status.PublishAt)
t.Errorf(
"expected publishAt=2026-08-18T15:00:00Z, got %s",
body.Status.PublishAt,
)
}
if body.Status.PrivacyStatus != "private" {
t.Errorf("expected privacyStatus=private when scheduling, got %s", body.Status.PrivacyStatus)
t.Errorf(
"expected privacyStatus=private when scheduling, got %s",
body.Status.PrivacyStatus,
)
}
}
},
@@ -991,10 +1024,13 @@ func TestVideo_Update(t *testing.T) {
verify: func(r *http.Request) {
if r.Method == "PUT" {
if !strings.Contains(r.URL.Query().Get("part"), "recordingDetails") {
t.Errorf("expected part to contain recordingDetails, got %s", r.URL.Query().Get("part"))
t.Errorf(
"expected part to contain recordingDetails, got %s",
r.URL.Query().Get("part"),
)
}
var body youtube.Video
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
if err := json.UnmarshalRead(r.Body, &body); err != nil {
t.Fatalf("failed to decode update body: %v", err)
}
if !body.Status.Embeddable {
@@ -1004,7 +1040,10 @@ func TestVideo_Update(t *testing.T) {
t.Errorf("expected containsSyntheticMedia=true")
}
if body.RecordingDetails == nil || body.RecordingDetails.RecordingDate != "2024-01-01T00:00:00Z" {
t.Errorf("expected recordingDate=2024-01-01T00:00:00Z, got %+v", body.RecordingDetails)
t.Errorf(
"expected recordingDate=2024-01-01T00:00:00Z, got %+v",
body.RecordingDetails,
)
}
}
},