11using System . Net ;
2+ using System . Text ;
23using BtmsGateway . Middleware ;
34using BtmsGateway . Services . Routing ;
45using BtmsGateway . Test . TestUtils ;
@@ -11,6 +12,8 @@ namespace BtmsGateway.Test.Middleware;
1112
1213public class MessageDataTests
1314{
15+ private const string RequestBody = "<Envelope><Body><Root><Data>abc</Data><CorrelationId>correlation-id</CorrelationId></Root></Body></Envelope>" ;
16+
1417 private readonly DefaultHttpContext _httpContext = new ( )
1518 {
1619 Request =
@@ -19,10 +22,10 @@ public class MessageDataTests
1922 Scheme = "http" ,
2023 Method = "GET" ,
2124 Host = new HostString ( "localhost" , 123 ) ,
25+ Body = new MemoryStream ( Encoding . UTF8 . GetBytes ( RequestBody ) ) ,
2226 Headers =
2327 {
2428 new KeyValuePair < string , StringValues > ( "Content-Length" , "999" ) ,
25- new KeyValuePair < string , StringValues > ( "X-Correlation-ID" , "correlation-id" ) ,
2629 new KeyValuePair < string , StringValues > ( "X-Custom" , "custom" )
2730 }
2831 }
@@ -76,25 +79,6 @@ public async Task When_receiving_a_get_request_that_should_not_be_processed_Then
7679 messageData . ShouldProcessRequest . Should ( ) . BeFalse ( ) ;
7780 }
7881
79- [ Fact ]
80- public async Task When_receiving_a_routable_get_request_without_content_type_or_accept_headers_Then_it_should_break_up_the_request_parts ( )
81- {
82- _httpContext . Request . Path = new PathString ( "/cds/path" ) ;
83- _httpContext . Request . Query = new QueryCollection ( new Dictionary < string , StringValues > { { "a" , "b" } } ) ;
84-
85- var messageData = await MessageData . Create ( _httpContext . Request , Logger . None ) ;
86-
87- messageData . HttpString . Should ( ) . Be ( "GET http://localhost:123/cds/path?a=b HTTP/1.1 " ) ;
88- messageData . Method . Should ( ) . Be ( "GET" ) ;
89- messageData . Path . Should ( ) . Be ( "cds/path" ) ;
90- messageData . Url . Should ( ) . Be ( "http://localhost:123/cds/path?a=b" ) ;
91- messageData . CorrelationId . Should ( ) . Be ( "correlation-id" ) ;
92- messageData . OriginalContentType . Should ( ) . Be ( "" ) ;
93- messageData . OriginalContentAsString . Should ( ) . BeNull ( ) ;
94- messageData . ContentMap . ChedType . Should ( ) . BeNull ( ) ;
95- messageData . ContentMap . CountryCode . Should ( ) . BeNull ( ) ;
96- }
97-
9882 [ Fact ]
9983 public async Task When_receiving_a_routable_get_request_with_accept_header_Then_it_should_break_up_the_request_parts ( )
10084 {
@@ -110,7 +94,7 @@ public async Task When_creating_a_routable_get_request_without_host_header_Then_
11094 {
11195 var messageData = await MessageData . Create ( _httpContext . Request , Logger . None ) ;
11296
113- var request = messageData . CreateForwardingRequestAsOriginal ( "https://localhost:456/cds/path" , null ) ;
97+ var request = messageData . CreateOriginalSoapRequest ( "https://localhost:456/cds/path" , null ) ;
11498
11599 request . RequestUri ! . ToString ( ) . Should ( ) . Be ( "https://localhost:456/cds/path" ) ;
116100 request . Method . Should ( ) . Be ( HttpMethod . Get ) ;
@@ -126,7 +110,7 @@ public async Task When_creating_a_routable_get_request_with_host_header_Then_it_
126110 {
127111 var messageData = await MessageData . Create ( _httpContext . Request , Logger . None ) ;
128112
129- var request = messageData . CreateForwardingRequestAsOriginal ( "https://localhost:456/cds/path" , "host-header" ) ;
113+ var request = messageData . CreateOriginalSoapRequest ( "https://localhost:456/cds/path" , "host-header" ) ;
130114
131115 request . Headers . Count ( ) . Should ( ) . Be ( 3 ) ;
132116 request . Headers . GetValues ( "host" ) . Should ( ) . BeEquivalentTo ( "host-header" ) ;
@@ -136,10 +120,9 @@ public async Task When_creating_a_routable_get_request_with_host_header_Then_it_
136120 public async Task When_creating_a_routable_get_request_with_content_Then_it_should_create_forwarding_request ( )
137121 {
138122 _httpContext . Request . Headers . ContentType = "application/soap+xml" ;
139- _httpContext . Request . Body = new MemoryStream ( "<root><data>abc</data></root>"u8 . ToArray ( ) ) ;
140123 var messageData = await MessageData . Create ( _httpContext . Request , Logger . None ) ;
141124
142- var request = messageData . CreateForwardingRequestAsOriginal ( "https://localhost:456/cds/path" , null ) ;
125+ var request = messageData . CreateOriginalSoapRequest ( "https://localhost:456/cds/path" , null ) ;
143126
144127 request . Content . Should ( ) . BeNull ( ) ;
145128 request . Headers . Count ( ) . Should ( ) . Be ( 3 ) ;
@@ -154,7 +137,7 @@ public async Task When_creating_a_routable_get_request_with_accept_header_Then_i
154137 _httpContext . Request . Headers . Add ( new KeyValuePair < string , StringValues > ( "Accept" , "application/json" ) ) ;
155138 var messageData = await MessageData . Create ( _httpContext . Request , Logger . None ) ;
156139
157- var request = messageData . CreateForwardingRequestAsOriginal ( "https://localhost:456/cds/path" , null ) ;
140+ var request = messageData . CreateOriginalSoapRequest ( "https://localhost:456/cds/path" , null ) ;
158141
159142 request . Headers . Count ( ) . Should ( ) . Be ( 3 ) ;
160143 request . Headers . GetValues ( MessageData . CorrelationIdHeaderName ) . Should ( ) . BeEquivalentTo ( "correlation-id" ) ;
@@ -168,19 +151,17 @@ public async Task When_receiving_an_original_routable_post_request_Then_it_shoul
168151 _httpContext . Request . Method = "POST" ;
169152 _httpContext . Request . Path = new PathString ( "/cds/path" ) ;
170153 _httpContext . Request . Headers . ContentType = "application/soap+xml" ;
171- _httpContext . Request . Body = new MemoryStream ( "<root><data>abc</data></root>"u8 . ToArray ( ) ) ;
172154
173155 var messageData = await MessageData . Create ( _httpContext . Request , Logger . None ) ;
174156
175157 messageData . HttpString . Should ( ) . Be ( "POST http://localhost:123/cds/path HTTP/1.1 application/soap+xml" ) ;
176158 messageData . Method . Should ( ) . Be ( "POST" ) ;
177159 messageData . Path . Should ( ) . Be ( "cds/path" ) ;
178160 messageData . Url . Should ( ) . Be ( "http://localhost:123/cds/path" ) ;
179- messageData . CorrelationId . Should ( ) . Be ( "correlation-id" ) ;
180161 messageData . OriginalContentType . Should ( ) . Be ( "application/soap+xml" ) ;
181- messageData . OriginalContentAsString . Should ( ) . Be ( "<root><data>abc</data></root>" ) ;
182- messageData . ContentMap . ChedType . Should ( ) . Be ( "" ) ;
183- messageData . ContentMap . CountryCode . Should ( ) . Be ( "" ) ;
162+ messageData . OriginalSoapContent . SoapString . Should ( ) . Be ( RequestBody ) ;
163+ messageData . ContentMap . EntryReference . Should ( ) . BeNull ( ) ;
164+ messageData . ContentMap . CountryCode . Should ( ) . BeNull ( ) ;
184165 }
185166
186167 [ Fact ]
@@ -189,13 +170,13 @@ public async Task When_receiving_an_original_routable_post_request_with_mappable
189170 _httpContext . Request . Method = "POST" ;
190171 _httpContext . Request . Path = new PathString ( "/cds/path" ) ;
191172 _httpContext . Request . Headers . ContentType = "application/soap+xml" ;
192- _httpContext . Request . Body = new MemoryStream ( "<root><ched>CHEDPP</ched ><DispatchCountryCode>NI</DispatchCountryCode></root >"u8 . ToArray ( ) ) ;
173+ _httpContext . Request . Body = new MemoryStream ( "<s:Envelope xmlns:s= \" http://soap \" ><s:Body><ALVSClearanceRequest><Header><EntryReference>ALVSCDSTEST00000000688</EntryReference ><DispatchCountryCode>NI</DispatchCountryCode><CorrelationId>123456789</CorrelationId></Header></ALVSClearanceRequest></s:Body></s:Envelope >"u8 . ToArray ( ) ) ;
193174
194175 var messageData = await MessageData . Create ( _httpContext . Request , Logger . None ) ;
195176
196- messageData . OriginalContentAsString . Should ( ) . Be ( "<root><ched>CHEDPP</ched><DispatchCountryCode>NI</DispatchCountryCode></root>" ) ;
197- messageData . ContentMap . ChedType . Should ( ) . Be ( "CHEDPP" ) ;
177+ messageData . ContentMap . EntryReference . Should ( ) . Be ( "ALVSCDSTEST00000000688" ) ;
198178 messageData . ContentMap . CountryCode . Should ( ) . Be ( "NI" ) ;
179+ messageData . ContentMap . CorrelationId . Should ( ) . Be ( "123456789" ) ;
199180 }
200181
201182 [ Fact ]
@@ -204,15 +185,14 @@ public async Task When_creating_a_routable_original_post_request_with_host_heade
204185 _httpContext . Request . Method = "POST" ;
205186 _httpContext . Request . Path = new PathString ( "/cds/path" ) ;
206187 _httpContext . Request . Headers . ContentType = "application/soap+xml" ;
207- _httpContext . Request . Body = new MemoryStream ( "<root><data>abc</data></root>"u8 . ToArray ( ) ) ;
208188 var messageData = await MessageData . Create ( _httpContext . Request , Logger . None ) ;
209189
210- var request = messageData . CreateForwardingRequestAsOriginal ( "https://localhost:456/cds/path" , "host-header" ) ;
190+ var request = messageData . CreateOriginalSoapRequest ( "https://localhost:456/cds/path" , "host-header" ) ;
211191
212192 request . RequestUri ! . ToString ( ) . Should ( ) . Be ( "https://localhost:456/cds/path" ) ;
213193 request . Method . Should ( ) . Be ( HttpMethod . Post ) ;
214194 request . Version . Should ( ) . Be ( Version . Parse ( "1.1" ) ) ;
215- ( await request . Content ! . ReadAsStringAsync ( ) ) . Should ( ) . Be ( "<root><data>abc</data></root>" ) ;
195+ ( await request . Content ! . ReadAsStringAsync ( ) ) . Should ( ) . Be ( RequestBody ) ;
216196 request . Content ! . Headers . ContentType ! . ToString ( ) . Should ( ) . Be ( "application/soap+xml; charset=utf-8" ) ;
217197 request . Headers . Count ( ) . Should ( ) . Be ( 4 ) ;
218198 request . Headers . GetValues ( MessageData . CorrelationIdHeaderName ) . Should ( ) . BeEquivalentTo ( "correlation-id" ) ;
@@ -227,12 +207,11 @@ public async Task When_creating_a_routable_converted_post_request_Then_it_should
227207 _httpContext . Request . Method = "POST" ;
228208 _httpContext . Request . Path = new PathString ( "/cds/path" ) ;
229209 _httpContext . Request . Headers . ContentType = "application/soap+xml" ;
230- _httpContext . Request . Body = new MemoryStream ( "<Envelope><Body><root><data>abc</data></root></Body></Envelope>"u8 . ToArray ( ) ) ;
231210 var messageData = await MessageData . Create ( _httpContext . Request , Logger . None ) ;
232211
233- var request = messageData . CreateConvertedForwardingRequest ( "https://localhost:456/cds/path" , null , "root " ) ;
212+ var request = messageData . CreateConvertedJsonRequest ( "https://localhost:456/cds/path" , null , "Root " ) ;
234213
235- ( await request . Content ! . ReadAsStringAsync ( ) ) . LinuxLineEndings ( ) . Should ( ) . Be ( "{\n \" data\" : \" abc\" \n }" ) ;
214+ ( await request . Content ! . ReadAsStringAsync ( ) ) . LinuxLineEndings ( ) . Should ( ) . Be ( "{\n \" data\" : \" abc\" , \n \" correlationId \" : \" correlation-id \" \n }" ) ;
236215 request . Content ! . Headers . ContentType ! . ToString ( ) . Should ( ) . Be ( "application/json; charset=utf-8" ) ;
237216 request . Headers . Count ( ) . Should ( ) . Be ( 3 ) ;
238217 request . Headers . GetValues ( "Accept" ) . Should ( ) . BeEquivalentTo ( "application/json" ) ;
@@ -245,7 +224,6 @@ public async Task When_populating_a_response_with_content_and_existing_date_Then
245224 _httpContext . Request . Method = "POST" ;
246225 _httpContext . Request . Path = new PathString ( "/cds/path" ) ;
247226 _httpContext . Request . Headers . ContentType = "application/soap+xml" ;
248- _httpContext . Request . Body = new MemoryStream ( "<Envelope><Body><root><data>abc</data></root></Body></Envelope>"u8 . ToArray ( ) ) ;
249227 var messageData = await MessageData . Create ( _httpContext . Request , Logger . None ) ;
250228 var responseBody = new MemoryStream ( ) ;
251229 _httpContext . Response . Body = responseBody ;
0 commit comments