31
31
import static org .hamcrest .Matchers .not ;
32
32
import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
33
33
import static org .junit .jupiter .api .Assertions .assertThrows ;
34
+ import static org .mockito .ArgumentMatchers .anyString ;
35
+ import static org .mockito .BDDMockito .given ;
36
+ import static org .mockito .Mockito .mock ;
34
37
35
38
import io .swagger .v3 .oas .models .servers .Server ;
36
39
import io .swagger .v3 .oas .models .servers .ServerVariable ;
44
47
import org .zaproxy .zap .extension .openapi .OpenApiExceptions .EmptyDefinitionException ;
45
48
import org .zaproxy .zap .extension .openapi .OpenApiExceptions .InvalidUrlException ;
46
49
import org .zaproxy .zap .extension .openapi .network .RequestModel ;
50
+ import org .zaproxy .zap .model .Context ;
47
51
48
52
/** Unit test for {@link SwaggerConverter}. */
49
53
class SwaggerConverterUnitTest extends AbstractOpenApiTest {
@@ -1117,7 +1121,7 @@ void shouldUsePathServers() throws Exception {
1117
1121
String definition = getHtml ("openapi_path_servers.yaml" );
1118
1122
SwaggerConverter converter = new SwaggerConverter (definition , null );
1119
1123
// When
1120
- List <RequestModel > requests = converter .getRequestModels ();
1124
+ List <RequestModel > requests = converter .getRequestModels (null );
1121
1125
// Then
1122
1126
assertThat (converter .getErrorMessages (), is (empty ()));
1123
1127
assertThat (
@@ -1136,7 +1140,7 @@ void shouldUseTargetUrlForPathServers() throws Exception {
1136
1140
SwaggerConverter converter =
1137
1141
new SwaggerConverter ("/v2/" , "http://localhost/definition/" , definition , null );
1138
1142
// When
1139
- List <RequestModel > requests = converter .getRequestModels ();
1143
+ List <RequestModel > requests = converter .getRequestModels (null );
1140
1144
// Then
1141
1145
assertThat (converter .getErrorMessages (), is (empty ()));
1142
1146
assertThat (
@@ -1153,7 +1157,7 @@ void shouldUseOperationServers() throws Exception {
1153
1157
String definition = getHtml ("openapi_operation_servers.yaml" );
1154
1158
SwaggerConverter converter = new SwaggerConverter (definition , null );
1155
1159
// When
1156
- List <RequestModel > requests = converter .getRequestModels ();
1160
+ List <RequestModel > requests = converter .getRequestModels (null );
1157
1161
// Then
1158
1162
assertThat (converter .getErrorMessages (), is (empty ()));
1159
1163
assertThat (
@@ -1177,7 +1181,7 @@ void shouldUseTargetUrlForOperationServers() throws Exception {
1177
1181
SwaggerConverter converter =
1178
1182
new SwaggerConverter ("/v2/" , "http://localhost/definition/" , definition , null );
1179
1183
// When
1180
- List <RequestModel > requests = converter .getRequestModels ();
1184
+ List <RequestModel > requests = converter .getRequestModels (null );
1181
1185
// Then
1182
1186
assertThat (converter .getErrorMessages (), is (empty ()));
1183
1187
assertThat (
@@ -1193,6 +1197,51 @@ void shouldUseTargetUrlForOperationServers() throws Exception {
1193
1197
"PATCH http://server8.localhost/v2/operations/with/servers" ));
1194
1198
}
1195
1199
1200
+ @ Test
1201
+ void shouldConvertAllEndpointsIfNoContextProvided () throws Exception {
1202
+ // Given
1203
+ String definition = getHtml ("openapi_paths_misc.yaml" );
1204
+ SwaggerConverter converter = new SwaggerConverter (definition , null );
1205
+ Context context = null ;
1206
+ // When
1207
+ List <RequestModel > requests = converter .getRequestModels (context );
1208
+ // Then
1209
+ assertThat (converter .getErrorMessages (), is (empty ()));
1210
+ assertThat (
1211
+ urlsOf (requests ),
1212
+ contains (
1213
+ "http://server.localhost/path/a/" ,
1214
+ "http://server.localhost/path/b/" ,
1215
+ "http://server.localhost/path/b/1/" ,
1216
+ "http://server.localhost/path/c/" ,
1217
+ "http://server.localhost/path/c/1/" ,
1218
+ "http://server.localhost/path/c/2/" ));
1219
+ }
1220
+
1221
+ @ Test
1222
+ void shouldNotConvertEndpointsExcludedFromContext () throws Exception {
1223
+ // Given
1224
+ String definition = getHtml ("openapi_paths_misc.yaml" );
1225
+ SwaggerConverter converter = new SwaggerConverter (definition , null );
1226
+ var context = mock (Context .class );
1227
+ given (context .isExcluded (anyString ()))
1228
+ .willAnswer (
1229
+ e -> {
1230
+ var uri = e .getArgument (0 ).toString ();
1231
+ return uri .contains ("/path/b/" ) || uri .contains ("/path/c/1/" );
1232
+ });
1233
+ // When
1234
+ List <RequestModel > requests = converter .getRequestModels (context );
1235
+ // Then
1236
+ assertThat (converter .getErrorMessages (), is (empty ()));
1237
+ assertThat (
1238
+ urlsOf (requests ),
1239
+ contains (
1240
+ "http://server.localhost/path/a/" ,
1241
+ "http://server.localhost/path/c/" ,
1242
+ "http://server.localhost/path/c/2/" ));
1243
+ }
1244
+
1196
1245
private static List <String > urlsOf (List <RequestModel > requests ) {
1197
1246
return requests .stream ().map (RequestModel ::getUrl ).collect (Collectors .toList ());
1198
1247
}
0 commit comments