@@ -3,6 +3,7 @@ package handler
33import (
44 "bytes"
55 "encoding/json"
6+ "errors"
67 "net/http"
78 "net/http/httptest"
89 "testing"
@@ -52,6 +53,42 @@ func TestCreateProcess(t *testing.T) {
5253
5354 assert .Equal (t , http .StatusBadRequest , w .Code )
5455 })
56+
57+ t .Run ("service error" , func (t * testing.T ) {
58+ mockService .On ("CreateProcessAsync" , mock .AnythingOfType ("*ports.CreateProcessRequest" )).
59+ Return (errors .New ("service error" )).Once ()
60+
61+ body := CreateProcessRequest {URL : "https://example.com/video.mp4" }
62+ jsonBody , _ := json .Marshal (body )
63+
64+ w := httptest .NewRecorder ()
65+ req , _ := http .NewRequest ("POST" , "/thumbs" , bytes .NewBuffer (jsonBody ))
66+ req .Header .Set ("Content-Type" , "application/json" )
67+ router .ServeHTTP (w , req )
68+
69+ assert .Equal (t , http .StatusInternalServerError , w .Code )
70+
71+ var response ErrorResponse
72+ err := json .Unmarshal (w .Body .Bytes (), & response )
73+ assert .NoError (t , err )
74+ assert .Equal (t , "service error" , response .Error )
75+ })
76+
77+ t .Run ("api bind json error" , func (t * testing.T ) {
78+ invalidJSON := []byte (`{"url": invalid-json}` )
79+
80+ w := httptest .NewRecorder ()
81+ req , _ := http .NewRequest ("POST" , "/thumbs" , bytes .NewBuffer (invalidJSON ))
82+ req .Header .Set ("Content-Type" , "application/json" )
83+ router .ServeHTTP (w , req )
84+
85+ assert .Equal (t , http .StatusBadRequest , w .Code )
86+
87+ var response ErrorResponse
88+ err := json .Unmarshal (w .Body .Bytes (), & response )
89+ assert .NoError (t , err )
90+ assert .Equal (t , "Invalid request format" , response .Error )
91+ })
5592}
5693
5794func TestUpdateProcess (t * testing.T ) {
@@ -82,6 +119,66 @@ func TestUpdateProcess(t *testing.T) {
82119
83120 assert .Equal (t , http .StatusOK , w .Code )
84121 })
122+
123+ t .Run ("api bind ID param error" , func (t * testing.T ) {
124+ body := UpdateProcessRequest {
125+ Status : "completed" ,
126+ ThumbnailPath : "path/to/thumbnail.jpg" ,
127+ }
128+ jsonBody , _ := json .Marshal (body )
129+
130+ w := httptest .NewRecorder ()
131+
132+ req , _ := http .NewRequest ("PUT" , "/thumbs/invalid-uuid" , bytes .NewBuffer (jsonBody ))
133+ req .Header .Set ("Content-Type" , "application/json" )
134+ router .ServeHTTP (w , req )
135+
136+ assert .Equal (t , http .StatusBadRequest , w .Code )
137+
138+ var response ErrorResponse
139+ err := json .Unmarshal (w .Body .Bytes (), & response )
140+ assert .NoError (t , err )
141+ assert .Equal (t , "Process ID is required" , response .Error )
142+ })
143+
144+ t .Run ("api bind json error" , func (t * testing.T ) {
145+ invalidJSON := []byte (`{"status": invalid-json}` )
146+
147+ w := httptest .NewRecorder ()
148+ req , _ := http .NewRequest ("PUT" , "/thumbs/" + mockedUUID .String (), bytes .NewBuffer (invalidJSON ))
149+ req .Header .Set ("Content-Type" , "application/json" )
150+ router .ServeHTTP (w , req )
151+
152+ assert .Equal (t , http .StatusBadRequest , w .Code )
153+
154+ var response ErrorResponse
155+ err := json .Unmarshal (w .Body .Bytes (), & response )
156+ assert .NoError (t , err )
157+ assert .Equal (t , "Invalid request format" , response .Error )
158+ })
159+
160+ t .Run ("service error" , func (t * testing.T ) {
161+ mockService .On ("UpdateProcess" , mock .AnythingOfType ("*ports.UpdateProcessRequest" )).
162+ Return (nil , errors .New ("service error" )).Once ()
163+
164+ body := UpdateProcessRequest {
165+ Status : "completed" ,
166+ ThumbnailPath : "path/to/thumbnail.jpg" ,
167+ }
168+ jsonBody , _ := json .Marshal (body )
169+
170+ w := httptest .NewRecorder ()
171+ req , _ := http .NewRequest ("PUT" , "/thumbs/" + mockedUUID .String (), bytes .NewBuffer (jsonBody ))
172+ req .Header .Set ("Content-Type" , "application/json" )
173+ router .ServeHTTP (w , req )
174+
175+ assert .Equal (t , http .StatusInternalServerError , w .Code )
176+
177+ var response ErrorResponse
178+ err := json .Unmarshal (w .Body .Bytes (), & response )
179+ assert .NoError (t , err )
180+ assert .Equal (t , "service error" , response .Error )
181+ })
85182}
86183
87184func TestListProcesses (t * testing.T ) {
0 commit comments