-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix exception handler lookup logic (#90)
* Fix exception handler lookup logic. Fixes #88 * Fix tests
- Loading branch information
1 parent
f357f14
commit 54b2159
Showing
2 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
...-server-jetty/src/test/groovy/io/micronaut/servlet/jetty/JettyExceptionHandlerSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package io.micronaut.servlet.jetty | ||
|
||
import groovy.transform.InheritConstructors | ||
import io.micronaut.context.annotation.Property | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.http.HttpRequest | ||
import io.micronaut.http.HttpResponse | ||
import io.micronaut.http.HttpStatus | ||
import io.micronaut.http.MutableHttpResponse | ||
import io.micronaut.http.annotation.Controller | ||
import io.micronaut.http.annotation.Get | ||
import io.micronaut.http.client.HttpClient | ||
import io.micronaut.http.client.annotation.Client | ||
import io.micronaut.http.client.exceptions.HttpClientResponseException | ||
import io.micronaut.http.server.exceptions.ExceptionHandler | ||
import io.micronaut.test.extensions.spock.annotation.MicronautTest | ||
import spock.lang.Specification | ||
|
||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@MicronautTest | ||
@Property(name = "spec.name", value = "JettyExceptionHandlerSpec") | ||
class JettyExceptionHandlerSpec extends Specification { | ||
|
||
@Inject | ||
@Client("/") | ||
HttpClient client | ||
|
||
void "test an exception handler for mutable request works"() { | ||
when: | ||
def resp = client.toBlocking().exchange("/exception") | ||
|
||
then: | ||
resp.status() == HttpStatus.OK | ||
} | ||
|
||
void "test an exception handler returning the body"() { | ||
when: | ||
client.toBlocking().retrieve("/exception/my") | ||
|
||
then: | ||
def ex = thrown(HttpClientResponseException) | ||
ex.response.status() == HttpStatus.INTERNAL_SERVER_ERROR | ||
ex.response.getBody().get() == "hello" | ||
} | ||
|
||
@Controller("/exception") | ||
static class ExceptionController { | ||
|
||
@Get | ||
void throwsEx() { | ||
throw new RuntimeException("bad") | ||
} | ||
|
||
@Get("/my") | ||
void throwsMy() { | ||
throw new MyException("bad") | ||
} | ||
} | ||
|
||
@Singleton | ||
static class MyExceptionHandler implements ExceptionHandler<MyException, String> { | ||
@Override | ||
String handle(HttpRequest request, MyException exception) { | ||
"hello" | ||
} | ||
} | ||
|
||
@Singleton | ||
@Requires(property = "spec.name", value = "JettyExceptionHandlerSpec") | ||
static class RuntimeExceptionHandler implements ExceptionHandler<RuntimeException, MutableHttpResponse<?>> { | ||
@Override | ||
MutableHttpResponse<?> handle(HttpRequest request, RuntimeException exception) { | ||
return HttpResponse.ok() | ||
} | ||
} | ||
|
||
@InheritConstructors | ||
static class MyException extends Exception {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters