@@ -129,6 +129,141 @@ func (cli *Client) Imposter(ctx context.Context, port int, replay bool) (*Impost
129129 return & imp , nil
130130}
131131
132+ // AddStub adds a new Stub without restarting its Imposter given the imposter's
133+ // port and the new stub's index, or simply to the end of the array if index < 0.
134+ //
135+ // See more information about this resource at:
136+ // http://www.mbtest.org/docs/api/overview#add-stub
137+ func (cli * Client ) AddStub (ctx context.Context , port , index int , stub Stub ) (* Imposter , error ) {
138+ p := fmt .Sprintf ("/imposters/%d/stubs" , port )
139+
140+ dto := map [string ]interface {}{"stub" : stub }
141+ if index >= 0 {
142+ dto ["index" ] = index
143+ }
144+ b , err := json .Marshal (dto )
145+ if err != nil {
146+ return nil , err
147+ }
148+
149+ req , err := cli .restCli .NewRequest (ctx , http .MethodPost , p , bytes .NewReader (b ), nil )
150+ if err != nil {
151+ return nil , err
152+ }
153+
154+ resp , err := cli .restCli .Do (req )
155+ if err != nil {
156+ return nil , err
157+ }
158+
159+ var imp Imposter
160+ if resp .StatusCode == http .StatusOK {
161+ if err := cli .restCli .DecodeResponseBody (resp .Body , & imp ); err != nil {
162+ return nil , err
163+ }
164+ } else {
165+ return nil , cli .decodeError (resp .Body )
166+ }
167+ return & imp , nil
168+ }
169+
170+ // OverwriteStub overwrites an existing Stub without restarting its Imposter,
171+ // where the stub index denotes the stub to be changed.
172+ //
173+ // See more information about this resouce at:
174+ // http://www.mbtest.org/docs/api/overview#change-stub
175+ func (cli * Client ) OverwriteStub (ctx context.Context , port , index int , stub Stub ) (* Imposter , error ) {
176+ p := fmt .Sprintf ("/imposters/%d/stubs/%d" , port , index )
177+
178+ b , err := json .Marshal (stub )
179+ if err != nil {
180+ return nil , err
181+ }
182+
183+ req , err := cli .restCli .NewRequest (ctx , http .MethodPut , p , bytes .NewReader (b ), nil )
184+ if err != nil {
185+ return nil , err
186+ }
187+
188+ resp , err := cli .restCli .Do (req )
189+ if err != nil {
190+ return nil , err
191+ }
192+
193+ var imp Imposter
194+ if resp .StatusCode == http .StatusOK {
195+ if err := cli .restCli .DecodeResponseBody (resp .Body , & imp ); err != nil {
196+ return nil , err
197+ }
198+ } else {
199+ return nil , cli .decodeError (resp .Body )
200+ }
201+ return & imp , nil
202+ }
203+
204+ // OverwriteAllStubs overwrites all existing Stubs without restarting their Imposter.
205+ //
206+ // See more information about this resource at:
207+ // http://www.mbtest.org/docs/api/overview#change-stubs
208+ func (cli * Client ) OverwriteAllStubs (ctx context.Context , port int , stubs []Stub ) (* Imposter , error ) {
209+ p := fmt .Sprintf ("/imposters/%d/stubs" , port )
210+
211+ b , err := json .Marshal (map [string ]interface {}{
212+ "stubs" : stubs ,
213+ })
214+ if err != nil {
215+ return nil , err
216+ }
217+
218+ req , err := cli .restCli .NewRequest (ctx , http .MethodPut , p , bytes .NewReader (b ), nil )
219+ if err != nil {
220+ return nil , err
221+ }
222+
223+ resp , err := cli .restCli .Do (req )
224+ if err != nil {
225+ return nil , err
226+ }
227+
228+ var imp Imposter
229+ if resp .StatusCode == http .StatusOK {
230+ if err := cli .restCli .DecodeResponseBody (resp .Body , & imp ); err != nil {
231+ return nil , err
232+ }
233+ } else {
234+ return nil , cli .decodeError (resp .Body )
235+ }
236+ return & imp , nil
237+ }
238+
239+ // RemoveStub removes a Stub without restarting its Imposter.
240+ //
241+ // See more information about this resource at:
242+ // http://www.mbtest.org/docs/api/overview#delete-stub
243+ func (cli * Client ) RemoveStub (ctx context.Context , port , index int ) (* Imposter , error ) {
244+ p := fmt .Sprintf ("/imposters/%d/stubs/%d" , port , index )
245+
246+ req , err := cli .restCli .NewRequest (ctx , http .MethodDelete , p , http .NoBody , nil )
247+ if err != nil {
248+ return nil , err
249+ }
250+
251+ resp , err := cli .restCli .Do (req )
252+ if err != nil {
253+ return nil , err
254+ }
255+
256+ var imp Imposter
257+ if resp .StatusCode == http .StatusOK {
258+ if err := cli .restCli .DecodeResponseBody (resp .Body , & imp ); err != nil {
259+ return nil , err
260+ }
261+ } else {
262+ return nil , cli .decodeError (resp .Body )
263+ }
264+ return & imp , nil
265+ }
266+
132267// Delete removes an Imposter configured on the given port and returns
133268// the deleted Imposter data, or an empty Imposter struct if one does not
134269// exist on the port.
@@ -169,7 +304,7 @@ func (cli *Client) Delete(ctx context.Context, port int, replay bool) (*Imposter
169304// See more information about this resource at:
170305// http://www.mbtest.org/docs/api/overview#delete-imposter-requests.
171306func (cli * Client ) DeleteRequests (ctx context.Context , port int ) (* Imposter , error ) {
172- p := fmt .Sprintf ("/imposters/%d/requests " , port )
307+ p := fmt .Sprintf ("/imposters/%d/savedProxyResponses " , port )
173308
174309 req , err := cli .restCli .NewRequest (ctx , http .MethodDelete , p , nil , nil )
175310 if err != nil {
0 commit comments