Skip to content

Commit

Permalink
Merge pull request sherpal#9 from raquo/fragment-encoding
Browse files Browse the repository at this point in the history
Apply URL decoding to fragment when parsing URLs. Fixes sherpal#7
  • Loading branch information
sherpal authored Mar 6, 2022
2 parents 1eb8782 + a26e41e commit 2034256
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 6 deletions.
5 changes: 3 additions & 2 deletions shared/.js/src/main/scala/urldsl/url/JSUrlStringParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ final class JSUrlStringParser(val rawUrl: String) extends UrlStringParser {

object JSUrlStringParser {

final lazy val jsUrlStringParserGenerator: UrlStringParserGenerator =
(rawUrl: String) => new JSUrlStringParser(rawUrl)
final lazy val jsUrlStringParserGenerator: UrlStringParserGenerator = new UrlStringParserGenerator {
def parser(rawUrl: String): UrlStringParser = new JSUrlStringParser(rawUrl)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ final class JavaNetUrlStringParser(val rawUrl: String) extends UrlStringParser {

object JavaNetUrlStringParser {

final lazy val javaNetUrlStringParserGenerator: UrlStringParserGenerator =
(rawUrl: String) => new JavaNetUrlStringParser(rawUrl)
final lazy val javaNetUrlStringParserGenerator: UrlStringParserGenerator = new UrlStringParserGenerator {
def parser(rawUrl: String): UrlStringParser = new JavaNetUrlStringParser(rawUrl)
}

}
2 changes: 1 addition & 1 deletion shared/src/main/scala/urldsl/language/Fragment.scala
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait Fragment[T, +E] extends UrlPart[T, E] {
def matchFragment(maybeFragment: MaybeFragment): Either[E, T]

def matchRawUrl(url: String, urlStringParserGenerator: UrlStringParserGenerator): Either[E, T] =
matchFragment(MaybeFragment(urlStringParserGenerator.parser(url).maybeFragment))
matchFragment(urlStringParserGenerator.parser(url).maybeFragmentObj)

/**
* Creates a fragment information from an instance of T.
Expand Down
2 changes: 1 addition & 1 deletion shared/src/main/scala/urldsl/url/UrlStringParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ trait UrlStringParser extends UrlStringDecoder {

final def segments: List[Segment] = decodePath(path)
final def params: Map[String, Param] = decodeParams(queryParametersString)
final def maybeFragmentObj: MaybeFragment = MaybeFragment(maybeFragment)
final def maybeFragmentObj: MaybeFragment = MaybeFragment(maybeFragment.map(decode(_)))

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ final class FragmentRawUrlSpec extends AnyFlatSpec with Matchers {

val stringFragment = fragment[String]

"Parsing a fragment with a space" should "read %20 as space" in {
val url = "http://localhost/#hey%20friends"
fragment[String].matchRawUrl(url) shouldBe Right("hey friends")
}

"Generating a fragment with a space" should "put %20 instead" in {
val str = "hey friends"
stringFragment.createPart(str) should be("#" ++ str.replaceAll(" ", "%20"))
Expand Down

0 comments on commit 2034256

Please sign in to comment.