11namespace ByteBard . AsyncAPI . Tests . Bindings . Http
22{
33 using System . Linq ;
4- using System . Net ;
54 using FluentAssertions ;
65 using ByteBard . AsyncAPI . Bindings ;
76 using ByteBard . AsyncAPI . Bindings . Http ;
@@ -156,7 +155,7 @@ public void V3_HttpMessageBinding_IncludesStatusCode()
156155 {
157156 Description = "response headers" ,
158157 } ,
159- StatusCode = HttpStatusCode . OK ,
158+ StatusCode = 200 ,
160159 } ) ;
161160
162161 // Act
@@ -179,7 +178,7 @@ public void V2_HttpMessageBinding_OmitsStatusCode()
179178 {
180179 Description = "response headers" ,
181180 } ,
182- StatusCode = HttpStatusCode . OK ,
181+ StatusCode = 200 ,
183182 } ) ;
184183
185184 // Act
@@ -189,5 +188,79 @@ public void V2_HttpMessageBinding_OmitsStatusCode()
189188 actual . Should ( ) . NotContain ( "statusCode" ) ;
190189 actual . Should ( ) . Contain ( "bindingVersion: 0.2.0" ) ;
191190 }
191+
192+ [ Test ]
193+ public void V3_HttpMessageBinding_ReadsStatusCode ( )
194+ {
195+ var yaml =
196+ """
197+ bindings:
198+ http:
199+ statusCode: 404
200+ """ ;
201+
202+ var settings = new AsyncApiReaderSettings { Bindings = BindingsCollection . Http } ;
203+ var message = new AsyncApiStringReader ( settings ) . ReadFragment < AsyncApiMessage > ( yaml , AsyncApiVersion . AsyncApi3_0 , out var diagnostics ) ;
204+
205+ diagnostics . Errors . Should ( ) . BeEmpty ( ) ;
206+ ( ( HttpMessageBinding ) message . Bindings [ "http" ] ) . StatusCode . Should ( ) . Be ( 404 ) ;
207+ }
208+
209+ [ TestCase ( "not-found" ) ]
210+ [ TestCase ( "200.5" ) ]
211+ public void V3_HttpMessageBinding_WithInvalidStatusCode_ReportsError ( string statusCode )
212+ {
213+ var yaml =
214+ "bindings:\n " +
215+ " http:\n " +
216+ " statusCode: " + statusCode + "\n " ;
217+
218+ var settings = new AsyncApiReaderSettings { Bindings = BindingsCollection . Http } ;
219+ new AsyncApiStringReader ( settings ) . ReadFragment < AsyncApiMessage > ( yaml , AsyncApiVersion . AsyncApi3_0 , out var diagnostics ) ;
220+
221+ diagnostics . Errors . Should ( ) . ContainSingle ( ) . Which . Message . Should ( ) . Contain ( "could not parse to integer" ) ;
222+ }
223+
224+ [ TestCase ( 100 ) ]
225+ [ TestCase ( 599 ) ]
226+ [ TestCase ( 425 ) ] // Too Early (RFC 8470), not a System.Net.HttpStatusCode member
227+ [ TestCase ( 499 ) ] // nginx client closed request
228+ [ TestCase ( 520 ) ] // Cloudflare unknown error
229+ public void V3_HttpMessageBinding_WithValidStatusCode_ReportsNoErrorAndRoundTrips ( int statusCode )
230+ {
231+ var yaml =
232+ "bindings:\n " +
233+ " http:\n " +
234+ " statusCode: " + statusCode + "\n " ;
235+
236+ var settings = new AsyncApiReaderSettings { Bindings = BindingsCollection . Http } ;
237+ var message = new AsyncApiStringReader ( settings ) . ReadFragment < AsyncApiMessage > ( yaml , AsyncApiVersion . AsyncApi3_0 , out var diagnostics ) ;
238+
239+ diagnostics . Errors . Should ( ) . BeEmpty ( ) ;
240+ ( ( HttpMessageBinding ) message . Bindings [ "http" ] ) . StatusCode . Should ( ) . Be ( statusCode ) ;
241+ message . SerializeAsYaml ( AsyncApiVersion . AsyncApi3_0 ) . Should ( ) . Contain ( "statusCode: " + statusCode ) ;
242+ }
243+
244+ [ TestCase ( 0 ) ]
245+ [ TestCase ( 42 ) ]
246+ [ TestCase ( 99 ) ]
247+ [ TestCase ( 600 ) ]
248+ [ TestCase ( - 200 ) ]
249+ public void V3_HttpMessageBinding_WithOutOfRangeStatusCode_ReportsErrorAndKeepsValue ( int statusCode )
250+ {
251+ var yaml =
252+ "bindings:\n " +
253+ " http:\n " +
254+ " statusCode: " + statusCode + "\n " ;
255+
256+ var settings = new AsyncApiReaderSettings { Bindings = BindingsCollection . Http } ;
257+ var message = new AsyncApiStringReader ( settings ) . ReadFragment < AsyncApiMessage > ( yaml , AsyncApiVersion . AsyncApi3_0 , out var diagnostics ) ;
258+
259+ var error = diagnostics . Errors . Should ( ) . ContainSingle ( ) . Subject ;
260+ error . Message . Should ( ) . Contain ( "must be between 100 and 599" ) ;
261+ error . Pointer . Should ( ) . EndWith ( "statusCode" ) ;
262+ ( ( HttpMessageBinding ) message . Bindings [ "http" ] ) . StatusCode . Should ( ) . Be ( statusCode ) ;
263+ message . SerializeAsYaml ( AsyncApiVersion . AsyncApi3_0 ) . Should ( ) . Contain ( "statusCode: " + statusCode ) ;
264+ }
192265 }
193266}
0 commit comments