Skip to content

Commit

Permalink
Add a test to convert a java URI to Uri & improve implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
adamw committed Nov 14, 2017
1 parent ab75366 commit 3e80847
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
10 changes: 1 addition & 9 deletions core/src/main/scala/com/softwaremill/sttp/Uri.scala
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ case class Uri(scheme: String,
queryFragments: Seq[QueryFragment],
fragment: Option[String]) {


def scheme(s: String): Uri = this.copy(scheme = s)

def userInfo(username: String): Uri =
Expand Down Expand Up @@ -196,14 +195,7 @@ object Uri {
Uri(scheme, None, host, Some(port), path, Vector.empty, None)
def apply(scheme: String, host: String, path: Seq[String]): Uri =
Uri(scheme, None, host, None, path, Vector.empty, None)
def apply(javaUri: URI): Uri = {
val scheme: String = javaUri.getScheme
val host: String = javaUri.getHost
val port: Option[Int] = Option(javaUri.getPort)
val path: Seq[String] = javaUri.getPath.split("/").toList.tail
val fragment: Option[String] = Option(javaUri.getFragment)
Uri(scheme, None, host, port, path, Vector.empty, fragment)
}
def apply(javaUri: URI): Uri = uri"${javaUri.toString}"

sealed trait QueryFragment
object QueryFragment {
Expand Down
7 changes: 7 additions & 0 deletions core/src/test/scala/com/softwaremill/sttp/UriTests.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.softwaremill.sttp

import java.net.URI

import com.softwaremill.sttp.Uri.{
QueryFragment,
QueryFragmentEncoding,
Expand Down Expand Up @@ -106,4 +108,9 @@ class UriTests extends FunSuite with Matchers {
testUri.copy(queryFragments = fragments).toString should endWith(expected)
}
}

test("should convert from java URI") {
val uriAsString = "https://sub.example.com:8080/a/b/xyz?p1=v1&p2=v2#f"
Uri(URI.create(uriAsString)).toString should be(uriAsString)
}
}

0 comments on commit 3e80847

Please sign in to comment.