-
Notifications
You must be signed in to change notification settings - Fork 301
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
54 additions
and
2 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
docs/adr |
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,26 @@ | ||
# 2. HTTP model conventions | ||
|
||
Date: 2019-10-24 | ||
|
||
## Context | ||
|
||
The classes which form the HTTP model (in the `sttp.model` package) vary in the conventions they use for error-handling | ||
and the parsing interface they offer. | ||
|
||
## Decision | ||
|
||
We want to unify the behavior of the HTTP model classes so that they work in a predictable and similar way. | ||
The conventions are: | ||
|
||
* `.toString` returns a representation of the model class in a format as in an HTTP request/response. For example, | ||
for an uri this will be `http://...`, for a header `[name]: [value]`, etc. | ||
* constructors of the model classes are private; instances should be created through methods on the companion objects. | ||
* `[SthCompanionObject].parse(serialized: String): Either[String, Sth]`: returns an error message or an instance of | ||
the model class | ||
* `[SthCompanionObject].apply(values)`: creates an instance of the model class; validates the input values and in case | ||
of an error, *throws an exception*. An error could be e.g. that the input values contain characters outside of | ||
the allowed range | ||
* `[SthCompanionObject].validated(...): Either[String, Sth]`: same as above, but doesn't throw exceptions. Instead, | ||
returns an error message or the model class instance | ||
* `[SthCompanionObject].notValidated(...): Sth`: creates the model type, without validation, and without throwing | ||
exceptions |
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
18 changes: 18 additions & 0 deletions
18
model/shared/src/test/scala/sttp/model/StatusCodeTests.scala
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,18 @@ | ||
package sttp.model | ||
|
||
import org.scalatest.{FlatSpec, Matchers} | ||
|
||
class StatusCodeTests extends FlatSpec with Matchers { | ||
it should "return a string description of the status code" in { | ||
StatusCode.Accepted.toString shouldBe "202" | ||
} | ||
|
||
it should "validate status codes" in { | ||
StatusCode.validated(8) shouldBe 'left | ||
StatusCode.validated(200) shouldBe Right(StatusCode.Ok) | ||
} | ||
|
||
it should "throw exceptions on invalid status codes" in { | ||
an[IllegalArgumentException] shouldBe thrownBy(StatusCode(8)) | ||
} | ||
} |