@@ -117,6 +117,13 @@ export class WireTestGenerator {
117117 return undefined ;
118118 }
119119
120+ /**
121+ * Checks if an example has an error response (non-2xx status code).
122+ */
123+ private isErrorResponse ( example : ExampleEndpointCall ) : boolean {
124+ return example . response . type === "error" ;
125+ }
126+
120127 /**
121128 * Converts a static IR example to a wire test example format.
122129 */
@@ -176,6 +183,7 @@ export class WireTestGenerator {
176183 example : WireTestExample ;
177184 service : HttpService ;
178185 exampleIndex : number ;
186+ isErrorResponse : boolean ;
179187 } > = [ ] ;
180188
181189 for ( const endpoint of endpoints ) {
@@ -192,7 +200,14 @@ export class WireTestGenerator {
192200 const staticExample = this . getStaticIrExample ( endpoint ) ;
193201 if ( staticExample ) {
194202 const wireTestExample = this . convertStaticExampleToWireTest ( endpoint , staticExample ) ;
195- endpointTestCases . push ( { endpoint, example : wireTestExample , service, exampleIndex : 0 } ) ;
203+ const isErrorResponse = this . isErrorResponse ( staticExample ) ;
204+ endpointTestCases . push ( {
205+ endpoint,
206+ example : wireTestExample ,
207+ service,
208+ exampleIndex : 0 ,
209+ isErrorResponse
210+ } ) ;
196211 }
197212 }
198213
@@ -224,6 +239,7 @@ export class WireTestGenerator {
224239 example : WireTestExample ;
225240 service : HttpService ;
226241 exampleIndex : number ;
242+ isErrorResponse : boolean ;
227243 } >
228244 ) : python . PythonFile {
229245 const statements : python . AstNode [ ] = [ ] ;
@@ -237,13 +253,14 @@ export class WireTestGenerator {
237253 statements . push ( this . createImportRegistration ( ) ) ;
238254
239255 // Add test functions for each endpoint
240- for ( const { endpoint, example, service, exampleIndex } of testCases ) {
256+ for ( const { endpoint, example, service, exampleIndex, isErrorResponse } of testCases ) {
241257 const testFunction = this . generateEndpointTestFunction (
242258 serviceName ,
243259 endpoint ,
244260 example ,
245261 service ,
246- exampleIndex
262+ exampleIndex ,
263+ isErrorResponse
247264 ) ;
248265 if ( testFunction ) {
249266 statements . push ( testFunction ) ;
@@ -269,6 +286,10 @@ export class WireTestGenerator {
269286 node . addReference ( python . reference ( { name : "get_client" , modulePath : [ ".conftest" ] } ) ) ;
270287 node . addReference ( python . reference ( { name : "verify_request_count" , modulePath : [ ".conftest" ] } ) ) ;
271288
289+ // Import ApiError from the SDK's core module for error response tests
290+ const orgName = this . context . config . organization ;
291+ node . addReference ( python . reference ( { name : "ApiError" , modulePath : [ orgName , "core" ] } ) ) ;
292+
272293 return node ;
273294 }
274295
@@ -281,7 +302,8 @@ export class WireTestGenerator {
281302 endpoint : HttpEndpoint ,
282303 example : WireTestExample ,
283304 service : HttpService ,
284- exampleIndex : number
305+ exampleIndex : number ,
306+ isErrorResponse : boolean
285307 ) : python . Method | null {
286308 try {
287309 const testName = this . getTestFunctionName ( serviceName , endpoint ) ;
@@ -303,14 +325,30 @@ export class WireTestGenerator {
303325 // Generate the API call AST directly
304326 const apiCallAst = this . generateApiCallAst ( endpoint , example ) ;
305327
306- // For streaming endpoints, wrap the call in a for loop to consume the iterator
307- // This is necessary because streaming methods return lazy generators that don't
308- // execute the HTTP request until iterated
309- if ( this . isStreamingEndpoint ( endpoint ) ) {
310- statements . push ( python . codeBlock ( `for _ in ${ apiCallAst . toString ( ) } :` ) ) ;
311- statements . push ( python . codeBlock ( " pass" ) ) ;
328+ // For error responses, wrap in pytest.raises() to expect the exception
329+ if ( isErrorResponse ) {
330+ // For streaming endpoints, we need to consume the iterator inside pytest.raises
331+ if ( this . isStreamingEndpoint ( endpoint ) ) {
332+ statements . push (
333+ python . codeBlock (
334+ `with pytest.raises(ApiError):\n for _ in ${ apiCallAst . toString ( ) } :\n pass`
335+ )
336+ ) ;
337+ } else {
338+ statements . push (
339+ python . codeBlock ( `with pytest.raises(ApiError):\n ${ apiCallAst . toString ( ) } ` )
340+ ) ;
341+ }
312342 } else {
313- statements . push ( apiCallAst ) ;
343+ // For streaming endpoints, wrap the call in a for loop to consume the iterator
344+ // This is necessary because streaming methods return lazy generators that don't
345+ // execute the HTTP request until iterated
346+ if ( this . isStreamingEndpoint ( endpoint ) ) {
347+ statements . push ( python . codeBlock ( `for _ in ${ apiCallAst . toString ( ) } :` ) ) ;
348+ statements . push ( python . codeBlock ( " pass" ) ) ;
349+ } else {
350+ statements . push ( apiCallAst ) ;
351+ }
314352 }
315353
316354 // Verify request count using test ID for filtering
@@ -529,7 +567,15 @@ export class WireTestGenerator {
529567 basePath = "/" + basePath ;
530568 }
531569
570+ // Strip URL fragment - fragments are never sent to the server in HTTP requests
571+ // e.g., "/oauth2/token#refresh" -> "/oauth2/token"
572+ const fragmentIndex = basePath . indexOf ( "#" ) ;
573+ if ( fragmentIndex !== - 1 ) {
574+ basePath = basePath . substring ( 0 , fragmentIndex ) ;
575+ }
576+
532577 // Substitute path parameters with actual values from WireMock mapping
578+ // Use the path WITHOUT fragment to look up the mapping, since mock-utils strips fragments
533579 const mappingKey = this . wiremockMappingKey ( {
534580 requestMethod : endpoint . method ,
535581 requestUrlPathTemplate : basePath
0 commit comments