-
Notifications
You must be signed in to change notification settings - Fork 427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fail when there's an unexpected response body #3095
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2380053
Fail when status = NoContent but there's a body
kciesielski ab6d4a9
Add a test case
kciesielski 42dda45
Refactor test
kciesielski a4e4790
Add 304 to status codes which shouldn't have body
kciesielski 0b518f6
Add verification to EndpointVerifier, test error outputs
kciesielski 6732ab2
Refactor for clarity
kciesielski 032e2d5
Add type hints for Scala 2, organize imports
kciesielski 0ab844c
Add missing dependency
kciesielski f3fe4a1
Adjust tests to avoid complains from Play
kciesielski 54bb7a9
Add one more test case for EndpointVerifier
kciesielski File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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 |
---|---|---|
@@ -1,9 +1,12 @@ | ||
package sttp.tapir.testing | ||
|
||
import io.circe.generic.auto._ | ||
import org.scalatest.flatspec.AnyFlatSpecLike | ||
import org.scalatest.matchers.should.Matchers | ||
import sttp.model.Method | ||
import sttp.model.{Method, StatusCode} | ||
import sttp.tapir._ | ||
import sttp.tapir.generic.auto._ | ||
import sttp.tapir.json.circe._ | ||
|
||
class EndpointVerifierTest extends AnyFlatSpecLike with Matchers { | ||
|
||
|
@@ -286,4 +289,42 @@ class EndpointVerifierTest extends AnyFlatSpecLike with Matchers { | |
|
||
result shouldBe Set() | ||
} | ||
|
||
it should "detect endpoints with body where status code doesn't allow a body" in { | ||
|
||
val e1 = endpoint.in("endpoint1_Err").out(stringBody).out(statusCode(StatusCode.NoContent)) | ||
val e2 = endpoint.in("endpoint2_Ok").out(stringBody).out(statusCode(StatusCode.BadRequest)) | ||
val e3 = endpoint.in("endpoint3_Err").out(stringBody).out(statusCode(StatusCode.NotModified)) | ||
val e4 = endpoint.in("endpoint4_ok").out(emptyOutputAs(NoContent)).out(statusCode(StatusCode.NoContent)) | ||
val e5 = endpoint | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would the checker pass for an endpoint such as: val e6 = endpoint
.in("endpoint6_ok")
.errorOut(
sttp.tapir.oneOf[ErrorInfo](
oneOfVariant(statusCode(StatusCode.NotFound).and(jsonBody[NotFound])),
oneOfVariant(statusCode(StatusCode.NoContent).and(emptyOutputAs[NoContent]))
)
) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea to check this, I have added a test. |
||
.in("endpoint5_err") | ||
.out(stringBody) | ||
.errorOut( | ||
sttp.tapir.oneOf[ErrorInfo]( | ||
oneOfVariant(statusCode(StatusCode.NotFound).and(jsonBody[NotFound])), | ||
oneOfVariant(statusCode(StatusCode.NoContent).and(jsonBody[NoContentData])) | ||
) | ||
) | ||
val e6 = endpoint | ||
.in("endpoint6_ok") | ||
.errorOut( | ||
sttp.tapir.oneOf[ErrorInfo]( | ||
oneOfVariant(statusCode(StatusCode.NotFound).and(jsonBody[NotFound])), | ||
oneOfVariant(statusCode(StatusCode.NoContent).and(emptyOutputAs(NoContent))) | ||
) | ||
) | ||
|
||
val result = EndpointVerifier(List(e1, e2, e3, e4, e5, e6)) | ||
|
||
result shouldBe Set( | ||
UnexpectedBodyError(e1, StatusCode.NoContent), | ||
UnexpectedBodyError(e3, StatusCode.NotModified), | ||
UnexpectedBodyError(e5, StatusCode.NoContent) | ||
) | ||
} | ||
} | ||
|
||
sealed trait ErrorInfo | ||
case class NotFound(what: String) extends ErrorInfo | ||
case object NoContent extends ErrorInfo | ||
case class NoContentData(msg: String) extends ErrorInfo |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it would be better to say:
The status code XYZ doesn't allow a body
Also ... what if the response body is defined as an
Option
? Or if the body is returned as an empty string""
- shouldn't we special case on this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would be hard to check in this general function. The body is of type
B
which is backend-dependent (likeakka.http.scaladsl.model.HttpEntity
).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, forget it then :)