Skip to content

Commit b344704

Browse files
committed
Merge pull request #91 from aoberoi/log-jdk
logs jdk version in user-agent, fixes #68
2 parents b8b8159 + ba75e81 commit b344704

File tree

3 files changed

+57
-40
lines changed

3 files changed

+57
-40
lines changed

src/main/java/com/opentok/util/HttpClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ public Builder apiUrl(String apiUrl) {
300300

301301
public HttpClient build() {
302302
this.config = new AsyncHttpClientConfig.Builder()
303-
.setUserAgent("Opentok-Java-SDK/"+Version.VERSION)
303+
.setUserAgent("Opentok-Java-SDK/" + Version.VERSION + " JRE/" + System.getProperty("java.version"))
304304
.addRequestFilter(new PartnerAuthRequestFilter(this.apiKey, this.apiSecret))
305305
.build();
306306
// NOTE: not thread-safe, config could be modified by another thread here?

src/test/java/com/opentok/test/Helpers.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@
1414
import java.util.Formatter;
1515
import java.util.HashMap;
1616
import java.util.Map;
17+
18+
import com.github.tomakehurst.wiremock.client.RequestPatternBuilder;
19+
import com.opentok.constants.Version;
1720
import org.apache.commons.codec.binary.Base64;
1821

1922
import javax.crypto.Mac;
2023
import javax.crypto.spec.SecretKeySpec;
2124
import java.net.URLDecoder;
2225

26+
import static com.github.tomakehurst.wiremock.client.WireMock.matching;
27+
import static com.github.tomakehurst.wiremock.client.WireMock.verify;
28+
2329
public class Helpers {
2430

2531
private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
@@ -46,6 +52,16 @@ public static boolean verifyTokenSignature(String token, String apiSecret) throw
4652
return (signature.equals(signData(decodedParts[1], apiSecret)));
4753
}
4854

55+
public static void verifyPartnerAuth(int apiKey, String apiSecret) {
56+
verify(RequestPatternBuilder.allRequests()
57+
.withHeader("X-TB-PARTNER-AUTH", matching(apiKey + ":" + apiSecret)));
58+
}
59+
60+
public static void verifyUserAgent() {
61+
verify(RequestPatternBuilder.allRequests()
62+
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/" + Version.VERSION + ".*JRE/" + System.getProperty("java.version") + ".*")));
63+
}
64+
4965
private static Map<String, String> decodeFormData(String formData) throws UnsupportedEncodingException {
5066
Map<String, String> decodedFormData = new HashMap<String, String>();
5167
String[] pairs = formData.split("\\&");

src/test/java/com/opentok/test/OpenTokTest.java

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ public void testCreateDefaultSession() throws OpenTokException {
8787

8888
verify(postRequestedFor(urlMatching("/session/create"))
8989
.withRequestBody(matching(".*p2p.preference=enabled.*"))
90-
.withRequestBody(matching(".*archiveMode=manual.*"))
91-
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey + ":" + this.apiSecret))
92-
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
90+
.withRequestBody(matching(".*archiveMode=manual.*")));
91+
Helpers.verifyPartnerAuth(this.apiKey, this.apiSecret);
92+
Helpers.verifyUserAgent();
9393
}
9494

9595
@Test
@@ -116,9 +116,9 @@ public void testCreateRoutedSession() throws OpenTokException {
116116

117117
verify(postRequestedFor(urlMatching("/session/create"))
118118
// NOTE: this is a pretty bad way to verify, ideally we can decode the body and then query the object
119-
.withRequestBody(matching(".*p2p.preference=disabled.*"))
120-
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret))
121-
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
119+
.withRequestBody(matching(".*p2p.preference=disabled.*")));
120+
Helpers.verifyPartnerAuth(this.apiKey, this.apiSecret);
121+
Helpers.verifyUserAgent();
122122
}
123123

124124
@Test
@@ -146,9 +146,9 @@ public void testCreateLocationHintSession() throws OpenTokException {
146146

147147
verify(postRequestedFor(urlMatching("/session/create"))
148148
// TODO: this is a pretty bad way to verify, ideally we can decode the body and then query the object
149-
.withRequestBody(matching(".*location="+locationHint+".*"))
150-
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret))
151-
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
149+
.withRequestBody(matching(".*location="+locationHint+".*")));
150+
Helpers.verifyPartnerAuth(this.apiKey, this.apiSecret);
151+
Helpers.verifyUserAgent();
152152
}
153153

154154
@Test
@@ -176,9 +176,9 @@ public void testCreateAlwaysArchivedSession() throws OpenTokException {
176176

177177
verify(postRequestedFor(urlMatching("/session/create"))
178178
// TODO: this is a pretty bad way to verify, ideally we can decode the body and then query the object
179-
.withRequestBody(matching(".*archiveMode=always.*"))
180-
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey + ":" + this.apiSecret))
181-
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/" + Version.VERSION + ".*")));
179+
.withRequestBody(matching(".*archiveMode=always.*")));
180+
Helpers.verifyPartnerAuth(this.apiKey, this.apiSecret);
181+
Helpers.verifyUserAgent();
182182
}
183183

184184
@Test(expected = InvalidArgumentException.class)
@@ -400,9 +400,9 @@ public void testGetArchive() throws OpenTokException {
400400
assertEquals("http://tokbox.com.archive2.s3.amazonaws.com/123456%2F"+archiveId +"%2Farchive.mp4?Expires=13951" +
401401
"94362&AWSAccessKeyId=AKIAI6LQCPIXYVWCQV6Q&Signature=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", archive.getUrl());
402402

403-
verify(getRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive/"+archiveId))
404-
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret))
405-
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
403+
verify(getRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive/"+archiveId)));
404+
Helpers.verifyPartnerAuth(this.apiKey, this.apiSecret);
405+
Helpers.verifyUserAgent();
406406
}
407407

408408
// TODO: test get archive failure scenarios
@@ -505,9 +505,9 @@ public void testListArchives() throws OpenTokException {
505505
assertThat(archives.get(0), instanceOf(Archive.class));
506506
assertEquals("ef546c5a-4fd7-4e59-ab3d-f1cfb4148d1d", archives.get(0).getId());
507507

508-
verify(getRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive"))
509-
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret))
510-
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
508+
verify(getRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive")));
509+
Helpers.verifyPartnerAuth(this.apiKey, this.apiSecret);
510+
Helpers.verifyUserAgent();
511511
}
512512

513513
// TODO: test list archives with count and offset
@@ -541,11 +541,11 @@ public void testStartArchive() throws OpenTokException {
541541
assertEquals(sessionId, archive.getSessionId());
542542
assertNotNull(archive.getId());
543543

544-
verify(postRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive"))
544+
verify(postRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive")));
545545
// TODO: find a way to match JSON without caring about spacing
546546
//.withRequestBody(matching(".*"+".*"))
547-
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret))
548-
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
547+
Helpers.verifyPartnerAuth(this.apiKey, this.apiSecret);
548+
Helpers.verifyUserAgent();
549549
}
550550

551551
@Test
@@ -576,11 +576,11 @@ public void testStartArchiveWithName() throws OpenTokException {
576576
assertEquals(name, archive.getName());
577577
assertNotNull(archive.getId());
578578

579-
verify(postRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive"))
579+
verify(postRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive")));
580580
// TODO: find a way to match JSON without caring about spacing
581581
//.withRequestBody(matching(".*"+".*"))
582-
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret))
583-
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
582+
Helpers.verifyPartnerAuth(this.apiKey, this.apiSecret);
583+
Helpers.verifyUserAgent();
584584
}
585585

586586
@Test
@@ -612,11 +612,11 @@ public void testStartVoiceOnlyArchive() throws OpenTokException {
612612
assertEquals(sessionId, archive.getSessionId());
613613
assertNotNull(archive.getId());
614614

615-
verify(postRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive"))
615+
verify(postRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive")));
616616
// TODO: find a way to match JSON without caring about spacing
617617
//.withRequestBody(matching(".*"+".*"))
618-
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret))
619-
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
618+
Helpers.verifyPartnerAuth(this.apiKey, this.apiSecret);
619+
Helpers.verifyUserAgent();
620620
}
621621

622622
@Test
@@ -648,11 +648,11 @@ public void testStartComposedArchive() throws OpenTokException {
648648
assertNotNull(archive.getId());
649649
assertEquals(OutputMode.COMPOSED, archive.getOutputMode());
650650

651-
verify(postRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive"))
651+
verify(postRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive")));
652652
// TODO: find a way to match JSON without caring about spacing
653653
//.withRequestBody(matching(".*"+".*"))
654-
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret))
655-
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
654+
Helpers.verifyPartnerAuth(this.apiKey, this.apiSecret);
655+
Helpers.verifyUserAgent();
656656
}
657657

658658
@Test
@@ -685,11 +685,11 @@ public void testStartIndividualArchive() throws OpenTokException {
685685
assertNotNull(archive.getId());
686686
assertEquals(OutputMode.INDIVIDUAL, archive.getOutputMode());
687687

688-
verify(postRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive"))
688+
verify(postRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive")));
689689
// TODO: find a way to match JSON without caring about spacing
690690
//.withRequestBody(matching(".*"+".*"))
691-
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret))
692-
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
691+
Helpers.verifyPartnerAuth(this.apiKey, this.apiSecret);
692+
Helpers.verifyUserAgent();
693693
}
694694

695695
// TODO: test start archive with name
@@ -722,9 +722,9 @@ public void testStopArchive() throws OpenTokException {
722722
assertEquals("SESSIONID", archive.getSessionId());
723723
assertEquals(archiveId, archive.getId());
724724

725-
verify(postRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive/"+archiveId+"/stop"))
726-
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret))
727-
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
725+
verify(postRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive/"+archiveId+"/stop")));
726+
Helpers.verifyPartnerAuth(this.apiKey, this.apiSecret);
727+
Helpers.verifyUserAgent();
728728
}
729729

730730
// TODO: test stop archive failure scenarios
@@ -739,9 +739,9 @@ public void testDeleteArchive() throws OpenTokException {
739739

740740
sdk.deleteArchive(archiveId);
741741

742-
verify(deleteRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive/"+archiveId))
743-
.withHeader("X-TB-PARTNER-AUTH", matching(this.apiKey+":"+this.apiSecret))
744-
.withHeader("User-Agent", matching(".*Opentok-Java-SDK/"+ Version.VERSION+".*")));
742+
verify(deleteRequestedFor(urlMatching("/v2/partner/"+this.apiKey+"/archive/"+archiveId)));
743+
Helpers.verifyPartnerAuth(this.apiKey, this.apiSecret);
744+
Helpers.verifyUserAgent();
745745
}
746746

747747
// TODO: test delete archive failure scenarios
@@ -820,4 +820,5 @@ public void testDeleteArchive() throws OpenTokException {
820820

821821
assertNotNull(archive);
822822
}
823+
823824
}

0 commit comments

Comments
 (0)