mirror of
https://github.com/Kingsrook/qqq.git
synced 2025-07-18 05:01:07 +00:00
Fix test (good catch - 200 for GET /api/, and 404 for bad version doc pages)
This commit is contained in:
@ -104,6 +104,10 @@ public class GenerateOpenApiSpecAction extends AbstractQActionFunction<GenerateO
|
||||
QInstance qInstance = QContext.getQInstance();
|
||||
|
||||
ApiInstanceMetaData apiInstanceMetaData = ApiInstanceMetaData.of(qInstance);
|
||||
if(!apiInstanceMetaData.getSupportedVersions().contains(new APIVersion(version)))
|
||||
{
|
||||
throw (new QException("[" + version + "] is not a supported API Version."));
|
||||
}
|
||||
|
||||
OpenAPI openAPI = new OpenAPI()
|
||||
.withVersion("3.0.3")
|
||||
|
@ -482,6 +482,12 @@ public class QJavalinApiHandler
|
||||
QBrandingMetaData branding = qInstance.getBranding();
|
||||
ApiInstanceMetaData apiInstanceMetaData = ApiInstanceMetaData.of(qInstance);
|
||||
|
||||
if(!apiInstanceMetaData.getSupportedVersions().contains(new APIVersion(version)))
|
||||
{
|
||||
doPathNotFound(context);
|
||||
return;
|
||||
}
|
||||
|
||||
//////////////////////////////////
|
||||
// read html from resource file //
|
||||
//////////////////////////////////
|
||||
@ -1634,6 +1640,24 @@ public class QJavalinApiHandler
|
||||
public static void respondWithError(Context context, HttpStatus.Code statusCode, String errorMessage)
|
||||
{
|
||||
context.status(statusCode.getCode());
|
||||
|
||||
try
|
||||
{
|
||||
String accept = context.header("accept");
|
||||
if(accept != null && accept.toLowerCase().startsWith(ContentType.HTML))
|
||||
{
|
||||
context.contentType(ContentType.HTML);
|
||||
context.result("Error: " + errorMessage);
|
||||
return;
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
///////////////////////////
|
||||
// just do default thing //
|
||||
///////////////////////////
|
||||
}
|
||||
|
||||
context.result(JsonUtils.toJson(Map.of("error", errorMessage)));
|
||||
}
|
||||
|
||||
|
@ -36,6 +36,7 @@ import com.kingsrook.qqq.backend.core.model.metadata.fields.QFieldType;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.Capability;
|
||||
import com.kingsrook.qqq.backend.core.model.metadata.tables.QTableMetaData;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
|
||||
@ -158,4 +159,17 @@ class GenerateOpenApiSpecActionTest extends BaseTest
|
||||
assertTrue(apiPaths.stream().noneMatch(s -> s.contains("/internalName/")));
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*******************************************************************************
|
||||
**
|
||||
*******************************************************************************/
|
||||
@Test
|
||||
void testBadVersion()
|
||||
{
|
||||
assertThatThrownBy(() -> new GenerateOpenApiSpecAction().execute(new GenerateOpenApiSpecInput().withVersion("NotAVersion")))
|
||||
.isInstanceOf(QException.class)
|
||||
.hasMessageContaining("not a supported API Version");
|
||||
}
|
||||
|
||||
}
|
@ -147,9 +147,20 @@ class QJavalinApiHandlerTest extends BaseTest
|
||||
{
|
||||
assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request(method, BASE_URL + "/api/" + VERSION + "/notATable/").asString());
|
||||
assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request(method, BASE_URL + "/api/" + VERSION + "/notATable/notAnId").asString());
|
||||
assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request(method, BASE_URL + "/api/").asString());
|
||||
assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request(method, BASE_URL + "/api/foo").asString());
|
||||
assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request(method, BASE_URL + "/api/" + VERSION + "/person/1/2").asString());
|
||||
assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request(method, BASE_URL + "/api/foo").asString());
|
||||
|
||||
if(method.equals("get"))
|
||||
{
|
||||
//////////////////////////////////////////////
|
||||
// this path returns the doc site for a GET //
|
||||
//////////////////////////////////////////////
|
||||
assertErrorResponse(HttpStatus.OK_200, null, Unirest.request(method, BASE_URL + "/api/").asString());
|
||||
}
|
||||
else
|
||||
{
|
||||
assertErrorResponse(HttpStatus.NOT_FOUND_404, "Could not find any resources at path", Unirest.request(method, BASE_URL + "/api/").asString());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user