Skip to content

Commit

Permalink
Simplifies Scala examples and adds its Java counterparts.
Browse files Browse the repository at this point in the history
  • Loading branch information
pfcoperez committed Oct 1, 2019
1 parent 4ee67cf commit 324f7fb
Show file tree
Hide file tree
Showing 7 changed files with 209 additions and 146 deletions.
14 changes: 10 additions & 4 deletions docs/src/main/paradox/common/http-model.md
Expand Up @@ -73,10 +73,13 @@ There are certain environments where it is easy to inadvertently print, write or

To avoid accidentally leaking such information, these fields are omitted from @apidoc[HttpRequest] `toString` output.

If needed, it is possible to alter this behavior using type classes as shown in the following examples:
If needed, it is possible to define a custom string representation including all fields as shown in the following example:

Scala
: @@snip [HttpRequestShow.scala]($test$/scala/docs/http/scaladsl/HttpRequestShow.scala)
: @@snip [HttpRequestDetailedStringExampleSpec.scala]($test$/scala/docs/http/scaladsl/HttpRequestDetailedStringExampleSpec.scala)

Java
: @@snip [HttpRequestDetailedStringExampleTest.java]($test$/java/docs/http/javadsl/HttpRequestDetailedStringExampleTest.java)

@@@

Expand Down Expand Up @@ -127,10 +130,13 @@ There are certain environments where it is easy to inadvertently print, write or

To avoid accidentally leaking such information, these fields are omitted from @apidoc[HttpResponse] `toString` output.

If needed, it is possible to alter this behavior using type classes as shown in the following examples:
If needed, it is possible to define a custom string representation including all fields as shown in the following example:

Scala
: @@snip [HttpResponseShow.scala]($test$/scala/docs/http/scaladsl/HttpResponseShow.scala)
: @@snip [HttpResponseDetailedStringExampleSpec.scala]($test$/scala/docs/http/scaladsl/HttpResponseDetailedStringExampleSpec.scala)

Java
: @@snip [HttpResponseDetailedStringExampleTest.java]($test$/java/docs/http/javadsl/HttpResponseDetailedStringExampleTest.java)

@@@

Expand Down
@@ -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()));

}

}
@@ -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()));

}

}
@@ -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 docs/src/test/scala/docs/http/scaladsl/HttpRequestShow.scala

This file was deleted.

@@ -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 docs/src/test/scala/docs/http/scaladsl/HttpResponseShow.scala

This file was deleted.

0 comments on commit 324f7fb

Please sign in to comment.