Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Simplifies Scala examples and adds its Java counterparts.
- Loading branch information
Showing
7 changed files
with
209 additions
and
146 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
51 changes: 51 additions & 0 deletions
51
docs/src/test/java/docs/http/javadsl/HttpRequestDetailedStringExampleTest.java
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,51 @@ | ||
/* | ||
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package docs.http.javadsl; | ||
|
||
import akka.http.javadsl.model.*; | ||
import akka.http.javadsl.model.headers.Authorization; | ||
import org.junit.Test; | ||
import static org.junit.Assert.*; | ||
|
||
import java.util.Arrays; | ||
|
||
public class HttpRequestDetailedStringExampleTest { | ||
|
||
// Custom string representation which includes headers | ||
public String toDetailedString(HttpRequest request) { | ||
|
||
HttpMethod method = request.method(); | ||
Uri uri = request.getUri(); | ||
Iterable<HttpHeader> headers = request.getHeaders(); | ||
RequestEntity entity = request.entity(); | ||
HttpProtocol protocol = request.protocol(); | ||
|
||
return String.format("HttpRequest(%s, %s, %s, %s, %s)", method, uri, headers, entity, protocol); | ||
} | ||
|
||
@Test | ||
public void headersInCustomStringRepresentation() { | ||
|
||
// An HTTP header containing Personal Identifying Information | ||
Authorization piiHeader = Authorization.basic("user", "password"); | ||
|
||
// An HTTP entity containing Personal Identifying Information | ||
HttpEntity.Strict piiBody = HttpEntities.create("This body contains information about [user]"); | ||
|
||
HttpRequest httpRequestWithHeadersAndBody = HttpRequest.create() | ||
.withEntity(piiBody) | ||
.withHeaders(Arrays.asList(piiHeader)); | ||
|
||
// Our custom string representation includes body and headers string representations... | ||
assertTrue(toDetailedString(httpRequestWithHeadersAndBody).contains(piiHeader.toString())); | ||
assertTrue(toDetailedString(httpRequestWithHeadersAndBody).contains(piiBody.toString())); | ||
|
||
// ... while default `toString` doesn't. | ||
assertFalse(httpRequestWithHeadersAndBody.toString().contains(piiHeader.toString())); | ||
assertFalse(httpRequestWithHeadersAndBody.toString().contains(piiBody.toString())); | ||
|
||
} | ||
|
||
} |
53 changes: 53 additions & 0 deletions
53
docs/src/test/java/docs/http/javadsl/HttpResponseDetailedStringExampleTest.java
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,53 @@ | ||
/* | ||
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package docs.http.javadsl; | ||
|
||
import akka.http.javadsl.model.*; | ||
import akka.http.javadsl.model.headers.Authorization; | ||
import org.junit.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
public class HttpResponseDetailedStringExampleTest { | ||
|
||
// Custom string representation which includes headers | ||
public String toDetailedString(HttpResponse response) { | ||
|
||
StatusCode status = response.status(); | ||
Iterable<HttpHeader> headers = response.getHeaders(); | ||
HttpEntity entity = response.entity(); | ||
HttpProtocol protocol = response.protocol(); | ||
|
||
|
||
return String.format("HttpResponse(%s, %s, %s, %s)", status, headers, entity, protocol); | ||
} | ||
|
||
@Test | ||
public void headersInCustomStringRepresentation() { | ||
|
||
// An HTTP header containing Personal Identifying Information | ||
Authorization piiHeader = Authorization.basic("user", "password"); | ||
|
||
// An HTTP entity containing Personal Identifying Information | ||
HttpEntity.Strict piiBody = HttpEntities.create("This body contains information about [user]"); | ||
|
||
HttpResponse httpResponseWithHeadersAndBody = HttpResponse.create() | ||
.withEntity(piiBody) | ||
.withHeaders(Arrays.asList(piiHeader)); | ||
|
||
// Our custom string representation includes body and headers string representations... | ||
assertTrue(toDetailedString(httpResponseWithHeadersAndBody).contains(piiHeader.toString())); | ||
assertTrue(toDetailedString(httpResponseWithHeadersAndBody).contains(piiBody.toString())); | ||
|
||
// ... while default `toString` doesn't. | ||
assertFalse(httpResponseWithHeadersAndBody.toString().contains(piiHeader.toString())); | ||
assertFalse(httpResponseWithHeadersAndBody.toString().contains(piiBody.toString())); | ||
|
||
} | ||
|
||
} |
47 changes: 47 additions & 0 deletions
47
docs/src/test/scala/docs/http/scaladsl/HttpRequestDetailedStringExampleSpec.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,47 @@ | ||
/* | ||
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package docs.http.scaladsl | ||
|
||
import akka.http.scaladsl.model.{ HttpEntity, HttpRequest } | ||
import akka.http.scaladsl.model.headers.{ Authorization, BasicHttpCredentials } | ||
import akka.testkit.AkkaSpec | ||
|
||
import scala.collection.immutable | ||
|
||
class HttpRequestDetailedStringExampleSpec extends AkkaSpec { | ||
|
||
// Custom string representation which includes headers | ||
def toDetailedString(request: HttpRequest): String = { | ||
import request._ | ||
s"""HttpRequest(${_1},${_2},${_3},${_4},${_5})""" | ||
} | ||
|
||
"Include headers in custom string representation" in { | ||
|
||
// An HTTP header containing Personal Identifying Information | ||
val piiHeader = Authorization(BasicHttpCredentials("user", "password")) | ||
|
||
// An HTTP entity containing Personal Identifying Information | ||
val piiBody: HttpEntity.Strict = | ||
"This body contains information about [user]" | ||
|
||
val httpRequestWithHeadersAndBody = | ||
HttpRequest(entity = piiBody, headers = immutable.Seq(piiHeader)) | ||
|
||
// Our custom string representation includes body and headers string representations... | ||
assert( | ||
toDetailedString(httpRequestWithHeadersAndBody) | ||
.contains(piiHeader.toString) | ||
) | ||
assert( | ||
toDetailedString(httpRequestWithHeadersAndBody).contains(piiBody.toString) | ||
) | ||
|
||
// ... while default `toString` doesn't. | ||
assert(!s"$httpRequestWithHeadersAndBody".contains(piiHeader.toString)) | ||
assert(!s"$httpRequestWithHeadersAndBody".contains(piiBody.toString)) | ||
} | ||
|
||
} |
71 changes: 0 additions & 71 deletions
71
docs/src/test/scala/docs/http/scaladsl/HttpRequestShow.scala
This file was deleted.
Oops, something went wrong.
48 changes: 48 additions & 0 deletions
48
docs/src/test/scala/docs/http/scaladsl/HttpResponseDetailedStringExampleSpec.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,48 @@ | ||
/* | ||
* Copyright (C) 2009-2019 Lightbend Inc. <https://www.lightbend.com> | ||
*/ | ||
|
||
package docs.http.scaladsl | ||
|
||
import akka.http.scaladsl.model.{ HttpEntity, HttpResponse } | ||
import akka.http.scaladsl.model.headers.{ Authorization, BasicHttpCredentials } | ||
import akka.testkit.AkkaSpec | ||
|
||
import scala.collection.immutable | ||
|
||
class HttpResponseDetailedStringExampleSpec extends AkkaSpec { | ||
|
||
// Custom string representation which includes headers | ||
def toDetailedString(response: HttpResponse): String = { | ||
import response._ | ||
s"""HttpResponse(${_1},${_2},${_3},${_4})""" | ||
} | ||
|
||
"Include headers in custom string representation" in { | ||
|
||
// An HTTP header containing Personal Identifying Information | ||
val piiHeader = Authorization(BasicHttpCredentials("user", "password")) | ||
|
||
// An HTTP entity containing Personal Identifying Information | ||
val piiBody: HttpEntity.Strict = | ||
"This body contains information about [user]" | ||
|
||
val httpResponseWithHeadersAndBody = | ||
HttpResponse(entity = piiBody, headers = immutable.Seq(piiHeader)) | ||
|
||
// Our custom string representation includes body and headers string representations... | ||
assert( | ||
toDetailedString(httpResponseWithHeadersAndBody) | ||
.contains(piiHeader.toString) | ||
) | ||
assert( | ||
toDetailedString(httpResponseWithHeadersAndBody) | ||
.contains(piiBody.toString) | ||
) | ||
|
||
// ... while default `toString` doesn't. | ||
assert(!s"$httpResponseWithHeadersAndBody".contains(piiHeader.toString)) | ||
assert(!s"$httpResponseWithHeadersAndBody".contains(piiBody.toString)) | ||
} | ||
|
||
} |
71 changes: 0 additions & 71 deletions
71
docs/src/test/scala/docs/http/scaladsl/HttpResponseShow.scala
This file was deleted.
Oops, something went wrong.