Skip to content

Commit a561e79

Browse files
author
Javier de Silóniz Sandino
committed
Using Kleisli to add headers to the http requests at exec time
1 parent a714e61 commit a561e79

18 files changed

Lines changed: 204 additions & 101 deletions

File tree

github4s/js/src/test/scala/github4s/utils/integration/GHAuthSpec.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class GHAuthSpec extends AsyncFlatSpec with Matchers with TestUtils {
4343
validNote,
4444
validClientId,
4545
invalidClientSecret)
46-
.exec[Future, SimpleHttpResponse]
46+
.execFuture(headerUserAgent)
4747

4848
testFutureIsLeft(response)
4949
}
@@ -52,7 +52,7 @@ class GHAuthSpec extends AsyncFlatSpec with Matchers with TestUtils {
5252
val response =
5353
Github().auth
5454
.authorizeUrl(validClientId, validRedirectUri, validScopes)
55-
.exec[Future, SimpleHttpResponse]
55+
.execFuture(headerUserAgent)
5656

5757
testFutureIsRight[Authorize](response, { r =>
5858
r.result.url.contains(validRedirectUri) shouldBe true
@@ -63,7 +63,7 @@ class GHAuthSpec extends AsyncFlatSpec with Matchers with TestUtils {
6363
"Auth >> GetAccessToken" should "return error on Left for invalid code value" in {
6464
val response = Github().auth
6565
.getAccessToken(validClientId, invalidClientSecret, "", validRedirectUri, "")
66-
.exec[Future, SimpleHttpResponse]
66+
.execFuture(headerUserAgent)
6767

6868
testFutureIsLeft(response)
6969
}

github4s/js/src/test/scala/github4s/utils/integration/GHGistsSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ class GHGistsSpec extends AsyncFlatSpec with Matchers with TestUtils {
3939
.newGist(validGistDescription,
4040
validGistPublic,
4141
Map(validGistFilename -> GistFile(validGistFileContent)))
42-
.exec[Future, SimpleHttpResponse]
42+
.execFuture(headerUserAgent)
4343

4444
testFutureIsRight[Gist](response, { r =>
4545
r.result.description shouldBe validGistDescription

github4s/js/src/test/scala/github4s/utils/integration/GHReposSpec.scala

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils {
3838
"Repos >> Get" should "return the expected name when valid repo is provided" in {
3939

4040
val response =
41-
Github(accessToken).repos.get(validRepoOwner, validRepoName).exec[Future, SimpleHttpResponse]
41+
Github(accessToken).repos.get(validRepoOwner, validRepoName).execFuture(headerUserAgent)
4242

4343
testFutureIsRight[Repository](response, { r =>
4444
r.result.name shouldBe validRepoName
@@ -48,9 +48,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils {
4848

4949
it should "return error when an invalid repo name is passed" in {
5050
val response =
51-
Github(accessToken).repos
52-
.get(validRepoOwner, invalidRepoName)
53-
.exec[Future, SimpleHttpResponse]
51+
Github(accessToken).repos.get(validRepoOwner, invalidRepoName).execFuture(headerUserAgent)
5452

5553
testFutureIsLeft(response)
5654
}
@@ -59,7 +57,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils {
5957
val response =
6058
Github(accessToken).repos
6159
.listCommits(validRepoOwner, validRepoName)
62-
.exec[Future, SimpleHttpResponse]
60+
.execFuture(headerUserAgent)
6361

6462
testFutureIsRight[List[Commit]](response, { r =>
6563
r.result.nonEmpty shouldBe true
@@ -71,7 +69,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils {
7169
val response =
7270
Github(accessToken).repos
7371
.listCommits(invalidRepoName, validRepoName)
74-
.exec[Future, SimpleHttpResponse]
72+
.execFuture(headerUserAgent)
7573

7674
testFutureIsLeft(response)
7775
}
@@ -80,7 +78,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils {
8078
val response =
8179
Github(accessToken).repos
8280
.listContributors(validRepoOwner, validRepoName)
83-
.exec[Future, SimpleHttpResponse]
81+
.execFuture(headerUserAgent)
8482

8583
testFutureIsRight[List[User]](response, { r =>
8684
r.result shouldNot be(empty)
@@ -92,7 +90,7 @@ class GHReposSpec extends AsyncFlatSpec with Matchers with TestUtils {
9290
val response =
9391
Github(accessToken).repos
9492
.listContributors(invalidRepoName, validRepoName)
95-
.exec[Future, SimpleHttpResponse]
93+
.execFuture(headerUserAgent)
9694

9795
testFutureIsLeft(response)
9896
}

github4s/js/src/test/scala/github4s/utils/integration/GHUsersSpec.scala

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class GHUsersSpec extends AsyncFlatSpec with Matchers with TestUtils {
3535
override implicit val executionContext = scala.concurrent.ExecutionContext.Implicits.global
3636

3737
"Users >> Get" should "return the expected login for a valid username" in {
38-
val response = Github(accessToken).users.get(validUsername).exec[Future, SimpleHttpResponse]
38+
val response = Github(accessToken).users.get(validUsername).execFuture(headerUserAgent)
3939

4040
testFutureIsRight[User](response, { r =>
4141
r.result.login shouldBe validUsername
@@ -44,18 +44,20 @@ class GHUsersSpec extends AsyncFlatSpec with Matchers with TestUtils {
4444
}
4545

4646
it should "return error on Left for invalid username" in {
47-
val response = Github(accessToken).users.get(invalidUsername).exec[Future, SimpleHttpResponse]
47+
val response = Github(accessToken).users.get(invalidUsername).execFuture(headerUserAgent)
48+
4849
testFutureIsLeft(response)
4950
}
5051

5152
"Users >> GetAuth" should "return error on Left when no accessToken is provided" in {
52-
val response = Github().users.getAuth.exec[Future, SimpleHttpResponse]
53+
val response = Github().users.getAuth.exec[Future, SimpleHttpResponse](headerUserAgent)
54+
5355
testFutureIsLeft(response)
5456
}
5557

5658
"Users >> GetUsers" should "return users for a valid since value" in {
5759
val response =
58-
Github(accessToken).users.getUsers(validSinceInt).exec[Future, SimpleHttpResponse]
60+
Github(accessToken).users.getUsers(validSinceInt).execFuture(headerUserAgent)
5961

6062
testFutureIsRight[List[User]](response, { r =>
6163
r.result.nonEmpty shouldBe true
@@ -65,7 +67,7 @@ class GHUsersSpec extends AsyncFlatSpec with Matchers with TestUtils {
6567

6668
it should "return an empty list when a invalid since value is provided" in {
6769
val response =
68-
Github(accessToken).users.getUsers(invalidSinceInt).exec[Future, SimpleHttpResponse]
70+
Github(accessToken).users.getUsers(invalidSinceInt).execFuture(headerUserAgent)
6971

7072
testFutureIsRight[List[User]](response, { r =>
7173
r.result.isEmpty shouldBe true

github4s/js/src/test/scala/github4s/utils/utils/TestUtils.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,9 @@ trait TestUtils extends Matchers {
5151
}
5252
}
5353

54-
val accessToken = Option(github4s.BuildInfo.token)
55-
def tokenHeader = "token " + accessToken.getOrElse("")
54+
val accessToken = Option(github4s.BuildInfo.token)
55+
def tokenHeader = "token " + accessToken.getOrElse("")
56+
val headerUserAgent = Map("user-agent" -> "github4s")
5657

5758
val validUsername = "rafaparadela"
5859
val invalidUsername = "GHInvalidaUserName"

github4s/jvm/src/test/scala/github4s/integration/GHAuthSpec.scala

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,17 @@ class GHAuthSpec extends FlatSpec with Matchers with TestUtils {
4040
validNote,
4141
validClientId,
4242
invalidClientSecret)
43-
.exec[Id, HttpResponse[String]]
43+
.exec[Id, HttpResponse[String]](headerUserAgent)
44+
4445
response should be('left)
4546
}
4647

4748
"Auth >> AuthorizeUrl" should "return the expected URL for valid username" in {
4849
val response =
4950
Github().auth
5051
.authorizeUrl(validClientId, validRedirectUri, validScopes)
51-
.exec[Id, HttpResponse[String]]
52+
.exec[Id, HttpResponse[String]](headerUserAgent)
53+
5254
response should be('right)
5355

5456
response.toOption map { r
@@ -61,7 +63,7 @@ class GHAuthSpec extends FlatSpec with Matchers with TestUtils {
6163
"Auth >> GetAccessToken" should "return error on Left for invalid code value" in {
6264
val response = Github().auth
6365
.getAccessToken(validClientId, invalidClientSecret, "", validRedirectUri, "")
64-
.exec[Id, HttpResponse[String]]
66+
.exec[Id, HttpResponse[String]](headerUserAgent)
6567
response should be('left)
6668
}
6769

github4s/jvm/src/test/scala/github4s/integration/GHGistsSpec.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ class GHGistsSpec extends FlatSpec with Matchers with TestUtils {
3838
.newGist(validGistDescription,
3939
validGistPublic,
4040
Map(validGistFilename -> GistFile(validGistFileContent)))
41-
.exec[Id, HttpResponse[String]]
41+
.exec[Id, HttpResponse[String]](headerUserAgent)
42+
4243
response should be('right)
4344
response.toOption map { r
4445
r.result.description shouldBe validGistDescription

github4s/jvm/src/test/scala/github4s/integration/GHReposSpec.scala

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,9 @@ class GHReposSpec extends FlatSpec with Matchers with TestUtils {
3737
"Repos >> Get" should "return the expected name when valid repo is provided" in {
3838

3939
val response =
40-
Github(accessToken).repos.get(validRepoOwner, validRepoName).exec[Id, HttpResponse[String]]
40+
Github(accessToken).repos
41+
.get(validRepoOwner, validRepoName)
42+
.exec[Id, HttpResponse[String]](headerUserAgent)
4143
response should be('right)
4244
response.toOption map { r
4345
r.result.name shouldBe validRepoName
@@ -47,14 +49,16 @@ class GHReposSpec extends FlatSpec with Matchers with TestUtils {
4749

4850
it should "return error when an invalid repo name is passed" in {
4951
val response =
50-
Github(accessToken).repos.get(validRepoOwner, invalidRepoName).exec[Id, HttpResponse[String]]
52+
Github(accessToken).repos
53+
.get(validRepoOwner, invalidRepoName)
54+
.exec[Id, HttpResponse[String]](headerUserAgent)
5155
response should be('left)
5256
}
5357

5458
"Repos >> ListCommits" should "return the expected list of commits for valid data" in {
5559
val response = Github(accessToken).repos
5660
.listCommits(validRepoOwner, validRepoName)
57-
.exec[Id, HttpResponse[String]]
61+
.exec[Id, HttpResponse[String]](headerUserAgent)
5862
response should be('right)
5963

6064
response.toOption map { r
@@ -66,15 +70,15 @@ class GHReposSpec extends FlatSpec with Matchers with TestUtils {
6670
it should "return error for invalid repo name" in {
6771
val response = Github(accessToken).repos
6872
.listCommits(invalidRepoName, validRepoName)
69-
.exec[Id, HttpResponse[String]]
73+
.exec[Id, HttpResponse[String]](headerUserAgent)
7074
response should be('left)
7175
}
7276

7377
"Repos >> ListContributors" should "return the expected list of contributors for valid data" in {
7478
val response =
7579
Github(accessToken).repos
7680
.listContributors(validRepoOwner, validRepoName)
77-
.exec[Id, HttpResponse[String]]
81+
.exec[Id, HttpResponse[String]](headerUserAgent)
7882
response should be('right)
7983

8084
response.toOption map { r
@@ -88,7 +92,7 @@ class GHReposSpec extends FlatSpec with Matchers with TestUtils {
8892
val response =
8993
Github(accessToken).repos
9094
.listContributors(invalidRepoName, validRepoName)
91-
.exec[Id, HttpResponse[String]]
95+
.exec[Id, HttpResponse[String]](headerUserAgent)
9296
response should be('left)
9397
}
9498

github4s/jvm/src/test/scala/github4s/integration/GHUsersSpec.scala

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ import scalaj.http.HttpResponse
3434
class GHUsersSpec extends FlatSpec with Matchers with TestUtils {
3535

3636
"Users >> Get" should "return the expected login for a valid username" in {
37-
val response = Github(accessToken).users.get(validUsername).exec[Id, HttpResponse[String]]
37+
val response =
38+
Github(accessToken).users.get(validUsername).exec[Id, HttpResponse[String]](headerUserAgent)
39+
3840
response should be('right)
3941
response.toOption map { r
4042
r.result.login shouldBe validUsername
@@ -43,17 +45,22 @@ class GHUsersSpec extends FlatSpec with Matchers with TestUtils {
4345
}
4446

4547
it should "return error on Left for invalid username" in {
46-
val response = Github(accessToken).users.get(invalidUsername).exec[Id, HttpResponse[String]]
48+
val response = Github(accessToken).users
49+
.get(invalidUsername)
50+
.exec[Id, HttpResponse[String]](headerUserAgent)
51+
4752
response should be('left)
4853
}
4954

5055
"Users >> GetAuth" should "return error on Left when no accessToken is provided" in {
51-
val response = Github().users.getAuth.exec[Id, HttpResponse[String]]
56+
val response = Github().users.getAuth.exec[Id, HttpResponse[String]](headerUserAgent)
5257
response should be('left)
5358
}
5459

5560
"Users >> GetUsers" should "return users for a valid since value" in {
56-
val response = Github(accessToken).users.getUsers(validSinceInt).exec[Id, HttpResponse[String]]
61+
val response = Github(accessToken).users
62+
.getUsers(validSinceInt)
63+
.exec[Id, HttpResponse[String]](headerUserAgent)
5764
response should be('right)
5865

5966
response.toOption map { r
@@ -64,7 +71,9 @@ class GHUsersSpec extends FlatSpec with Matchers with TestUtils {
6471

6572
it should "return an empty list when a invalid since value is provided" in {
6673
val response =
67-
Github(accessToken).users.getUsers(invalidSinceInt).exec[Id, HttpResponse[String]]
74+
Github(accessToken).users
75+
.getUsers(invalidSinceInt)
76+
.exec[Id, HttpResponse[String]](headerUserAgent)
6877
response should be('right)
6978

7079
response.toOption map { r

0 commit comments

Comments
 (0)