Skip to content

Commit

Permalink
LDEV-3947 http resource exists() stricter status code checks v3
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitzer committed May 20, 2024
1 parent 78570a5 commit 138ba20
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public boolean exists() {
try {
provider.read(this);
int code = getStatusCode();// getHttpMethod().getStatusCode();
return code != 404;
return code >= 200 && code <= 299;
}
catch (Exception e) {
return false;
Expand Down Expand Up @@ -126,7 +126,11 @@ public InputStream getInputStream() throws IOException {
throw ExceptionUtil.toIOException(e);
}
try {
return IOUtil.toBufferedInputStream(method.getContentAsStream());
int code = method.getStatusCode();
if (code >= 200 && code <= 299) return IOUtil.toBufferedInputStream(method.getContentAsStream());
else {
throw new IOException("HTTP request [" + getParentResource().toString() + " returned [" + code + "]");
}
}
finally {
HTTPEngine.closeEL(method);
Expand Down
50 changes: 50 additions & 0 deletions test/tickets/LDEV3947.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
component extends = "org.lucee.cfml.test.LuceeTestCase" labels="http" {

// requires LDEV-4861 to support rest httpmethod="get,head" and on update provider
// as the exists check does a HEAD request
variables.endpoint = "https://update.lucee.org/rest/update/provider/echoGet?";

function run( testresults , testbox ) {
describe( "testcase for LDEV-3947, http resource provider should error with non 20x status codes", function () {
it( title="Check reading an http resource",body = function ( currentSpec ) {
expect( fileRead("#variables.endpoint#") ).notToBeEmpty();
});

it( title="Check reading an http resource with 200 status code",body = function ( currentSpec ) {
expect( fileRead("#variables.endpoint#&statusCode=200") ).notToBeEmpty();
});

it( title="Check reading an http resource with 401 status code",body = function ( currentSpec ) {
expect(function(){
fileRead("#variables.endpoint#&statusCode=401");
}).toThrow();
});

it( title="Check reading an http resource with 403 status code",body = function ( currentSpec ) {
expect(function(){
fileRead("#variables.endpoint#&statusCode=403");
}).toThrow();
});

it( title="Check reading an http resource with 404 status code",body = function ( currentSpec ) {
expect(function(){
fileRead("#variables.endpoint#&statusCode=404");
}).toThrow();
});

it( title="Check reading an http resource with 500 status code",body = function ( currentSpec ) {
expect(function(){
fileRead("#variables.endpoint#&statusCode=500");
}).toThrow();
});

it( title="Check reading an http resource with 503 status code",body = function ( currentSpec ) {
expect(function(){
fileRead("#variables.endpoint#&statusCode=503");
}).toThrow();
});

});
}

}

0 comments on commit 138ba20

Please sign in to comment.