-
-
Notifications
You must be signed in to change notification settings - Fork 3
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
7 changed files
with
245 additions
and
42 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
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 |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/* | ||
* Copyright 2023 Creative Scala | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package krop.route | ||
|
||
import munit.FunSuite | ||
|
||
import scala.util.Failure | ||
import scala.util.Success | ||
|
||
class QueryParamSuite extends FunSuite { | ||
test("Required QueryParam succeeds if first value parses") { | ||
val qp = QueryParam("id", Param.int) | ||
assertEquals( | ||
qp.parse(Map("id" -> List("1"), "name" -> List("Van Gogh"))), | ||
Success(1) | ||
) | ||
assertEquals( | ||
qp.parse(Map("id" -> List("1", "foobar"), "name" -> List("Van Gogh"))), | ||
Success(1) | ||
) | ||
} | ||
|
||
test("Required QueryParam fails if first value fails to parse") { | ||
val qp = QueryParam("id", Param.int) | ||
assertEquals( | ||
qp.parse(Map("id" -> List("abc"))), | ||
Failure(QueryParseException.ValueParsingFailed("id", "abc", Param.int)) | ||
) | ||
} | ||
|
||
test("Required QueryParam fails if no values are found for name") { | ||
val qp = QueryParam("id", Param.int) | ||
assertEquals( | ||
qp.parse(Map("id" -> List())), | ||
Failure(QueryParseException.NoValuesForName("id")) | ||
) | ||
} | ||
|
||
test("Required QueryParam fails if name does not exist") { | ||
val qp = QueryParam("id", Param.int) | ||
assertEquals( | ||
qp.parse(Map("foo" -> List("1"))), | ||
Failure(QueryParseException.NoParameterWithName("id")) | ||
) | ||
} | ||
|
||
test("Optional QueryParam succeeds if first value parses") { | ||
val qp = QueryParam.optional("id", Param.int) | ||
assertEquals( | ||
qp.parse(Map("id" -> List("1"), "name" -> List("Van Gogh"))), | ||
Success(Some(1)) | ||
) | ||
assertEquals( | ||
qp.parse(Map("id" -> List("1", "foobar"), "name" -> List("Van Gogh"))), | ||
Success(Some(1)) | ||
) | ||
} | ||
|
||
test("Optional QueryParam fails if first value fails to parse") { | ||
val qp = QueryParam.optional("id", Param.int) | ||
assertEquals( | ||
qp.parse(Map("id" -> List("abc"))), | ||
Failure(QueryParseException.ValueParsingFailed("id", "abc", Param.int)) | ||
) | ||
} | ||
|
||
test("Optional QueryParam succeeds if no values are found for name") { | ||
val qp = QueryParam.optional("id", Param.int) | ||
assertEquals( | ||
qp.parse(Map("id" -> List())), | ||
Success(None) | ||
) | ||
} | ||
|
||
test("Optional QueryParam succeeds if name does not exist") { | ||
val qp = QueryParam.optional("id", Param.int) | ||
assertEquals( | ||
qp.parse(Map("foo" -> List("1"))), | ||
Success(None) | ||
) | ||
} | ||
} |
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
43 changes: 43 additions & 0 deletions
43
core/shared/src/main/scala/krop/route/QueryParseException.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,43 @@ | ||
/* | ||
* Copyright 2023 Creative Scala | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package krop.route | ||
|
||
/** Exception raised when query parsing fails. */ | ||
enum QueryParseException(message: String) extends Exception(message) { | ||
|
||
/** Query parameter parsing failed because no parameter with the given name | ||
* was found in the query parameters. | ||
*/ | ||
case NoParameterWithName(name: String) | ||
extends QueryParseException( | ||
s"There was no query parameter with the name ${name}." | ||
) | ||
|
||
/** Query parameter parsing failed because there was a parameter with the | ||
* given name in the query parameters, but that parameter was not associated | ||
* with any values. | ||
*/ | ||
case NoValuesForName(name: String) | ||
extends QueryParseException( | ||
s"There were no values associated with the name ${name}" | ||
) | ||
|
||
case ValueParsingFailed(name: String, value: String, param: Param[?]) | ||
extends QueryParseException( | ||
s"Parsing the value ${value} as ${param.describe} failed for the query parameter ${name}" | ||
) | ||
} |
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