-
Notifications
You must be signed in to change notification settings - Fork 41
Expand file tree
/
Copy pathCoreTest.java
More file actions
1231 lines (1062 loc) · 68.9 KB
/
Copy pathCoreTest.java
File metadata and controls
1231 lines (1062 loc) · 68.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2022-2025, FusionAuth, All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package io.fusionauth.http;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.net.Socket;
import java.net.URI;
import java.net.URLDecoder;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpRequest.BodyPublishers;
import java.net.http.HttpResponse.BodySubscribers;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.SecureRandom;
import java.security.cert.Certificate;
import java.time.Duration;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.InflaterInputStream;
import com.inversoft.net.ssl.SSLTools;
import com.inversoft.rest.RESTClient;
import com.inversoft.rest.TextResponseHandler;
import io.fusionauth.http.HTTPValues.Connections;
import io.fusionauth.http.HTTPValues.Headers;
import io.fusionauth.http.log.AccumulatingLogger;
import io.fusionauth.http.log.AccumulatingLoggerFactory;
import io.fusionauth.http.log.Level;
import io.fusionauth.http.server.CountingInstrumenter;
import io.fusionauth.http.server.HTTPHandler;
import io.fusionauth.http.server.HTTPListenerConfiguration;
import io.fusionauth.http.server.HTTPServer;
import io.fusionauth.http.server.HTTPServerConfiguration;
import org.testng.annotations.Test;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
/**
* Tests the HTTP server.
*
* @author Brian Pontarelli
*/
@SuppressWarnings("OptionalGetWithoutIsPresent")
public class CoreTest extends BaseTest {
public static final String ExpectedResponse = """
{
"version": "42"
}
"""
.replaceAll("\\s", "");
// This string is 16,640 characters long
public static final String LongString = "1234567890".repeat(1_664);
public static final String RequestBody = """
{
"message": "Hello World"
}
""";
@Test(dataProvider = "schemes")
public void badLanguage(String scheme) throws Exception {
HTTPHandler handler = (req, res) -> {
assertTrue(req.getLocales().isEmpty());
res.setStatus(200);
res.getOutputStream().close();
};
try (var client = makeClient(scheme, null); var ignore = makeServer(scheme, handler).start()) {
URI uri = makeURI(scheme, "");
HttpRequest request = HttpRequest.newBuilder()
.uri(uri)
.header(Headers.AcceptLanguage, "en, fr_bad;q=0.7")
.GET()
.build();
var response = client.send(request, r -> BodySubscribers.ofInputStream());
assertEquals(response.statusCode(), 200);
}
}
@Test
public void badPreambleButReset() throws Exception {
HTTPHandler handler = (req, res) -> {
assertNull(req.getHeader("Bad-Header"));
assertEquals(req.getHeader("Good-Header"), "Good-Header");
res.setStatus(200);
};
var instrumenter = new CountingInstrumenter();
// Use case: Send a malformed request, socket gets closed, maybe. Ensure server recovers and accepts another request.
try (var client = HttpClient.newHttpClient(); var ignore = makeServer("http", handler, instrumenter).start()) {
// Invalid request, missing Host header
// - This should cause the socket to be reset
sendBadRequest("""
GET / HTTP/1.1\r
X-Bad-Header: Bad-Header\r\r
""");
URI uri = makeURI("http", "");
HttpRequest request = HttpRequest.newBuilder()
.uri(uri)
.header("Good-Header", "Good-Header")
.GET()
.build();
var response = client.send(request, r -> BodySubscribers.ofString(StandardCharsets.UTF_8));
assertEquals(response.statusCode(), 200);
}
assertEquals(instrumenter.getBadRequests(), 1);
}
@Test(enabled = false)
public void certificateChain() throws Exception {
HTTPHandler handler = (req, res) -> {
res.setStatus(200);
res.getOutputStream().close();
};
try (var client = makeClient("https", null); var ignore = makeServer("https", handler).start()) {
URI uri = makeURI("https", "");
HttpRequest request = HttpRequest.newBuilder()
.uri(uri)
.GET()
.build();
var response = client.send(request, r -> BodySubscribers.ofInputStream());
assertEquals(response.statusCode(), 200);
var sslSession = response.sslSession().get();
var peerCerts = sslSession.getPeerCertificates();
// Verify that we received all intermediates, and can verify the chain all the way up to rootCertificate.
validateCertPath(rootCertificate, peerCerts);
}
}
@Test(dataProvider = "schemes")
public void emptyContentType(String scheme) throws Exception {
HTTPHandler handler = (req, res) -> {
assertNull(req.getContentType());
res.setStatus(200);
};
try (var client = makeClient(scheme, null); var ignore = makeServer(scheme, handler).start()) {
URI uri = makeURI(scheme, "");
var response = client.send(
HttpRequest.newBuilder().uri(uri).header(Headers.ContentType, "").POST(BodyPublishers.noBody()).build(),
r -> BodySubscribers.ofString(StandardCharsets.UTF_8)
);
assertEquals(response.statusCode(), 200);
}
}
@Test(dataProvider = "schemes")
public void emptyContentTypeWithEncoding(String scheme) throws Exception {
HTTPHandler handler = (req, res) -> {
assertEquals(req.getContentType(), "");
assertEquals(req.getCharacterEncoding(), StandardCharsets.UTF_16);
res.setStatus(200);
};
try (var client = makeClient(scheme, null); var ignore = makeServer(scheme, handler).start()) {
URI uri = makeURI(scheme, "");
var response = client.send(
HttpRequest.newBuilder().uri(uri).header(Headers.ContentType, "; charset=UTF-16").POST(BodyPublishers.noBody()).build(),
r -> BodySubscribers.ofString(StandardCharsets.UTF_8)
);
assertEquals(response.statusCode(), 200);
}
}
@Test(dataProvider = "schemes")
public void handlerFailureGet(String scheme) throws Exception {
HTTPHandler handler = (req, res) -> {
throw new IllegalStateException("Bad state");
};
try (var client = makeClient(scheme, null); var ignore = makeServer(scheme, handler).start()) {
URI uri = makeURI(scheme, "");
var response = client.send(
HttpRequest.newBuilder().uri(uri).GET().build(),
r -> BodySubscribers.ofString(StandardCharsets.UTF_8)
);
assertEquals(response.statusCode(), 500);
}
}
@Test(dataProvider = "schemes")
public void handlerFailurePost(String scheme) throws Exception {
HTTPHandler handler = (req, res) -> {
throw new IllegalStateException("Bad state");
};
try (var client = makeClient(scheme, null); var ignore = makeServer(scheme, handler).start()) {
URI uri = makeURI(scheme, "");
var response = client.send(
HttpRequest.newBuilder().uri(uri).header(Headers.ContentType, "application/json").POST(BodyPublishers.ofString(RequestBody)).build(),
r -> BodySubscribers.ofString(StandardCharsets.UTF_8)
);
assertEquals(response.statusCode(), 500);
}
}
@Test(dataProvider = "connections")
public void handler_sets_connection_response_header(String connection) throws Exception {
// The request handler sets a Connection header, the server should honor it
HTTPHandler handler = (req, res) -> {
res.setStatus(200);
res.setHeader(Headers.Connection, connection);
};
try (var client = makeClient("http", null); var ignore = makeServer("http", handler).start()) {
URI uri = makeURI("http", "");
HttpRequest request = HttpRequest.newBuilder()
.uri(uri)
.GET()
.build();
var response = client.send(request, r -> BodySubscribers.ofString(StandardCharsets.UTF_8));
assertEquals(response.statusCode(), 200);
assertEquals(response.headers().firstValue(Headers.Connection).get(), connection);
}
}
@Test(groups = "timeouts")
public void initialReadTimeout() {
// This test simulates if the client doesn't send bytes for the initial timeout
HTTPHandler handler = (req, res) -> {
byte[] response = "Hey, looks like the timeout didn't work!".getBytes(StandardCharsets.UTF_8);
res.setHeader(Headers.ContentLength, response.length + "");
res.setHeader(Headers.ContentType, "text/plain");
var os = res.getOutputStream();
os.write(response);
os.close();
};
var instrumenter = new CountingInstrumenter();
try (var ignore = makeServer("http", handler, instrumenter)
.withInitialReadTimeout(Duration.ofMillis(250))
.start();
var socket = new Socket("127.0.0.1", 4242)) {
// Open a socket to the server and then wait to write any bytes.
sleep(1_000);
var out = socket.getOutputStream();
// Now write something for the server to read
out.write("""
GET / HTTP/1.1\r
Host: localhost:42\r
Connection: close\r
Content-Length: 4\r
\r
body
""".getBytes());
out.flush();
var response = socket.getInputStream().readAllBytes();
assertEquals(response.length, 0, new String(response, StandardCharsets.UTF_8));
} catch (Exception ignore) {
// Expected
}
// Expect to have seen one connection closed due to a timeout.
// - Because we were not able to ready any bytes from the client, we have 0 accepted requests.
assertEquals(instrumenter.getAcceptedRequests(), 0);
assertEquals(instrumenter.getClosedConnections(), 1);
}
@Test(groups = "timeouts")
public void keepAliveTimeout() {
// This test only works with GET and the URLConnection because this setup will re-submit the same request if the Keep-Alive connection
// is terminated by the server
HTTPHandler handler = (req, res) -> {
assertNull(req.getContentType());
res.setStatus(200);
};
try (var ignore = makeServer("http", handler).withKeepAliveTimeoutDuration(Duration.ofSeconds(1)).start()) {
URI uri = makeURI("http", "");
var response = new RESTClient<>(Void.TYPE, Void.TYPE)
.url(uri.toString())
.connectTimeout(0)
.readTimeout(0)
.get()
.go();
if (response.status != 200) {
println(response.exception);
}
assertEquals(response.status, 200);
// This will cause the keep-alive on the server to expire and that means the socket will be dead but the client should receover
sleep(2_000L);
response = new RESTClient<>(Void.TYPE, Void.TYPE)
.url(uri.toString())
.connectTimeout(0)
.readTimeout(0)
.get()
.go();
if (response.status != 200) {
println(response.exception);
}
assertEquals(response.status, 200);
}
}
@Test
public void keepAlive_maxRequests() throws Exception {
// While using a persistent connection, exceed the configured maximum requests per connection.
// - Expect the request is closed as if we had reached a keep-alive timeout.
// Allow up to 10 requests per connection
int maxRequests = 10;
HTTPHandler handler = (req, res) -> res.setStatus(200);
try (var ignore = makeServer("http", handler)
.withMaxRequestsPerConnection(maxRequests)
.withKeepAliveTimeoutDuration(Duration.ofSeconds(60))
.start();
var client = makeClient("http", null)) {
URI uri = makeURI("http", "");
HttpRequest request = HttpRequest.newBuilder()
.uri(uri)
.GET()
.build();
// All but the last request will keep the 'keep-alive' response header.
// - The last request will be 'close'
for (int i = 1; i <= maxRequests; i++) {
var response = client.send(request, r -> BodySubscribers.ofString(StandardCharsets.UTF_8));
assertEquals(response.statusCode(), 200);
assertEquals(response.headers().firstValue(Headers.Connection).get(), maxRequests == i ? Connections.Close : Connections.KeepAlive);
}
// Note that this test is not actually proving we closed the socket. To do that I'd have to use a socket directly.
}
}
@Test(dataProvider = "schemes")
public void largeCSS(String scheme) throws Exception {
var css = Files.readString(Paths.get("src/test/resources/fontawesome-6.0.0.min.css"), StandardCharsets.UTF_8);
HTTPHandler handler = (req, res) -> {
res.setStatus(200);
try {
var out = res.getOutputStream();
out.write(css.getBytes(StandardCharsets.UTF_8));
out.close();
} catch (Throwable t) {
println(t);
}
};
try (var client = makeClient(scheme, null); var ignore = makeServer(scheme, handler).start()) {
URI uri = makeURI(scheme, "");
HttpRequest request = HttpRequest.newBuilder()
.uri(uri)
.GET()
.build();
var response = client.send(request, r -> BodySubscribers.ofString(StandardCharsets.UTF_8));
assertEquals(response.statusCode(), 200);
assertEquals(response.body(), css);
}
}
@Test(dataProvider = "schemes")
public void large_body(String scheme) throws Exception {
// Ensure that when the body bytes overflow from the initial "left over" bytes read
// during reading of the preamble, the remaining bytes read from the HTTPInputStream
// properly use offset and lengths when reading.
var value = "1234567890";
var valueLength = (new HTTPServerConfiguration().getRequestBufferSize() / value.length()) + 42;
var payload = "foo=" + value.repeat(valueLength);
byte[] bytes = payload.getBytes(StandardCharsets.UTF_8);
HTTPHandler handler = (req, res) -> {
res.setStatus(200);
res.setContentType("application/x-www-form-urlencoded; charset=UTF-8");
res.setContentLength(req.getBodyBytes().length);
res.getOutputStream().write(req.getBodyBytes());
res.getOutputStream().close();
// This will hose up the works - or it did until we fixed a bug in the HTTPRequest.getBodyBytes method.
req.getFormData();
// Ensure you can call this method and get the same value in return each time.
assertEquals(req.getBodyBytes(), req.getBodyBytes());
};
var instrumenter = new CountingInstrumenter();
try (var ignore = makeServer(scheme, handler, instrumenter).start();
var client = makeClient(scheme, null)) {
URI uri = makeURI(scheme, "");
var response = client.send(HttpRequest.newBuilder()
.uri(uri)
.POST(HttpRequest.BodyPublishers.ofString(payload))
.build(),
r -> BodySubscribers.ofByteArray());
assertEquals(response.statusCode(), 200);
assertEquals(response.body(), bytes);
// This assertion is always true unless we change what we are writing above. But it is here for reference.
assertEquals(bytes.length, 16_804);
// This should prove the PushbackInputStream isn't double counting.
// - We should expect the bytes read to be roughly equivalent to the payload length. We can add some bytes to account
// for the HTTP preamble, and for HTTPs additional overhead is incurred due to encryption. But if we are counting incorrectly
// due to bytes being pushed back and counted again, the numbers would be almost double.
// - So we should expect the bytes read to be within the ball bark of the payload length.
// - Note, this number will vary by HTTP and HTTPS due to the overhead of encryption, and can also vary by system.
// The lower boundary is the actual payload size, and the upper boundary is something reasonable that encompasses some of the sizes
// I've seen.
long bytesRead = instrumenter.getBytesRead();
assertTrue(bytesRead >= 16_804 && bytesRead <= 17_100);
}
}
@Test(dataProvider = "schemes")
public void large_headers(String scheme) throws Exception {
// Use case: Ensure we can read headers from the request, and send headers on the response that exceed the default request and repsonse buffer lengths.
// Ensure the headers in total exceed the response buffer sizes.
var requestBufferLength = new HTTPServerConfiguration().getRequestBufferSize();
var headersRequiredToExceedRequestBufferLength = (requestBufferLength / LongString.length()) + 1;
// Ensure the headers in total exceed the response buffer sizes.
var responseBufferLength = new HTTPServerConfiguration().getResponseBufferSize();
var headersRequiredToExceedResponseBufferLength = (responseBufferLength / LongString.length()) + 1;
// The server will return these larger headers on the HTTP response
HTTPHandler handler = (req, res) -> {
// Expect the headers sent by the client
for (int i = 0; i < headersRequiredToExceedRequestBufferLength; i++) {
assertEquals(req.getHeader("X-Huge-Header-" + i), LongString);
}
// Now write large headers back on the response
res.setHeader(Headers.ContentType, "text/plain");
res.setHeader(Headers.ContentLength, ExpectedResponse.getBytes().length + "");
for (int i = 0; i < headersRequiredToExceedResponseBufferLength; i++) {
res.setHeader("X-Huge-Header-" + i, LongString);
}
res.setStatus(200);
try {
OutputStream outputStream = res.getOutputStream();
outputStream.write(ExpectedResponse.getBytes());
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
};
try (var client = makeClient(scheme, null); var ignore = makeServer(scheme, handler).start()) {
URI uri = makeURI(scheme, "");
var builder = HttpRequest.newBuilder()
.uri(uri);
// The client is going to send these large headers on the request
for (int i = 0; i < headersRequiredToExceedRequestBufferLength; i++) {
builder.setHeader("X-Huge-Header-" + i, LongString);
}
var response = client.send(builder
.POST(BodyPublishers.ofString(RequestBody))
.build(),
r -> BodySubscribers.ofString(StandardCharsets.UTF_8)
);
assertEquals(response.statusCode(), 200);
// Ensure each header came back on the response
for (int i = 0; i < headersRequiredToExceedResponseBufferLength; i++) {
assertEquals(response.headers().firstValue("X-Huge-Header-" + i).get(), LongString);
}
}
}
@Test
public void logger() {
// Test replacement values and ensure we are handling special regex characters.
AccumulatingLogger logger = new AccumulatingLogger();
logger.setLevel(Level.Debug);
logger.info("Class name: [{}]", "io.fusionauth.http.Test$InnerClass");
// Expect that we do not encounter an exception.
String output = logger.toString();
assertTrue(output.endsWith("Class name: [io.fusionauth.http.Test$InnerClass]"));
}
@Test(dataProvider = "schemes")
public void partialWriteThenException(String scheme) throws Exception {
HTTPHandler handler = (req, res) -> {
res.setStatus(200);
res.getWriter().write("Here some body that should not be flushed");
throw new RuntimeException("Failure");
};
try (var client = makeClient(scheme, null); var ignore = makeServer(scheme, handler).start()) {
URI uri = makeURI(scheme, "");
var response = client.send(
HttpRequest.newBuilder()
.uri(uri)
.GET()
.build(),
r -> BodySubscribers.ofString(StandardCharsets.UTF_8)
);
assertEquals(response.statusCode(), 500);
}
}
@Test(dataProvider = "schemes")
public void partialWriteThenFlushThenException(String scheme) throws Exception {
HTTPHandler handler = (req, res) -> {
res.setStatus(200);
Writer writer = res.getWriter();
writer.write("Here some body that should not be flushed");
writer.flush();
throw new RuntimeException("Failure");
};
try (var client = makeClient(scheme, null); var ignore = makeServer(scheme, handler).start()) {
URI uri = makeURI(scheme, "");
var response = client.send(
HttpRequest.newBuilder()
.uri(uri)
.GET()
.build(),
r -> BodySubscribers.ofString(StandardCharsets.UTF_8)
);
assertEquals(response.statusCode(), 500);
}
}
@Test(dataProvider = "schemes", groups = "performance")
public void performance(String scheme) throws Exception {
HTTPHandler handler = (req, res) -> {
res.setHeader(Headers.ContentType, "text/plain");
res.setHeader(Headers.ContentLength, "16");
res.setStatus(200);
try {
OutputStream outputStream = res.getOutputStream();
outputStream.write(ExpectedResponse.getBytes());
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
};
int iterations = 100_000;
CountingInstrumenter instrumenter = new CountingInstrumenter();
try (var client = makeClient(scheme, null); var ignore = makeServer(scheme, handler, instrumenter).start()) {
URI uri = makeURI(scheme, "");
long start = System.currentTimeMillis();
for (int i = 0; i < iterations; i++) {
var response = client.send(
HttpRequest.newBuilder().uri(uri).GET().build(),
r -> BodySubscribers.ofString(StandardCharsets.UTF_8)
);
assertEquals(response.statusCode(), 200);
assertEquals(response.body(), ExpectedResponse);
if (i % 1_000 == 0) {
println(i);
}
}
long end = System.currentTimeMillis();
double average = (end - start) / (double) iterations;
println("Average linear request time is [" + average + "]ms");
}
assertEquals(instrumenter.getConnections(), 1);
}
@Test(dataProvider = "schemes", groups = "performance")
public void performanceNoKeepAlive(String scheme) throws Exception {
HTTPHandler handler = (req, res) -> {
res.setHeader(Headers.ContentType, "text/plain");
res.setHeader(Headers.ContentLength, "16");
res.setStatus(200);
try {
OutputStream outputStream = res.getOutputStream();
outputStream.write(ExpectedResponse.getBytes());
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
};
int iterations = 5_000;
int i = -1;
CountingInstrumenter instrumenter = new CountingInstrumenter();
try (var client = makeClient(scheme, null); var ignore = makeServer(scheme, handler, instrumenter).start()) {
try {
URI uri = makeURI(scheme, "");
long start = System.currentTimeMillis();
for (i = 0; i < iterations; i++) {
var response = client.send(
HttpRequest.newBuilder()
.uri(uri)
.header(Headers.Connection, Connections.Close)
.POST(BodyPublishers.noBody())
.build(),
r -> BodySubscribers.ofString(StandardCharsets.UTF_8)
);
if (i % 1_000 == 0) {
println(i);
}
assertEquals(response.statusCode(), 200);
assertEquals(response.body(), ExpectedResponse);
}
long end = System.currentTimeMillis();
double average = (end - start) / (double) iterations;
println("Average linear request time without keep-alive is [" + average + "]ms");
} catch (Exception e) {
StringBuilder threadDump = new StringBuilder();
for (Map.Entry<Thread, StackTraceElement[]> entry : Thread.getAllStackTraces().entrySet()) {
threadDump.append(entry.getKey()).append(" ").append(entry.getKey().getState()).append("\n");
for (StackTraceElement ste : entry.getValue()) {
threadDump.append("\tat ").append(ste).append("\n");
}
threadDump.append("\n");
}
println(threadDump);
throw e;
}
} catch (Exception e) {
println("Failed on iteration " + i);
throw e;
}
// Because we are not re-using the connections, we expect this to equal the iteration count.
assertEquals(instrumenter.getConnections(), iterations);
// getClosedConnections() should only represent closed connections due to errors.
assertEquals(instrumenter.getClosedConnections(), 0);
}
/**
* This test uses Restify in order to leverage the URLConnection implementation of the JDK. That implementation is not smart enough to
* realize that a socket in the connection pool that was using Keep-Alives with the server is potentially dead. Since we are shutting down
* the server and doing another request, this ensures that the server itself is sending a socket close signal back to the URLConnection
* and removing the socket form the connection pool.
*/
@Test(dataProvider = "schemes")
public void serverClosesSockets(String scheme) {
HTTPHandler handler = (req, res) -> {
res.setHeader(Headers.ContentType, "text/plain");
res.setHeader(Headers.ContentLength, "16");
res.setStatus(200);
try {
OutputStream outputStream = res.getOutputStream();
outputStream.write(ExpectedResponse.getBytes());
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
};
try (var ignore = makeServer(scheme, handler).start()) {
SSLTools.disableSSLValidation();
URI uri = makeURI(scheme, "");
var response = new RESTClient<>(String.class, String.class).url(uri.toString())
.connectTimeout(600_000)
.readTimeout(600_000)
.get()
.successResponseHandler(new TextResponseHandler())
.errorResponseHandler(new TextResponseHandler())
.go();
assertEquals(response.status, 200);
assertEquals(response.successResponse, ExpectedResponse);
} finally {
SSLTools.enableSSLValidation();
}
try (var ignore = makeServer(scheme, handler).start()) {
SSLTools.disableSSLValidation();
URI uri = makeURI(scheme, "");
var response = new RESTClient<>(String.class, String.class).url(uri.toString())
.connectTimeout(600_000)
.readTimeout(600_000)
.get()
.successResponseHandler(new TextResponseHandler())
.errorResponseHandler(new TextResponseHandler())
.go();
assertEquals(response.status, 200);
assertEquals(response.successResponse, ExpectedResponse);
} finally {
SSLTools.enableSSLValidation();
}
}
@Test(groups = "timeouts")
public void serverTimeout() throws Exception {
// This test simulates if the server has a long-running thread that doesn't write fast enough
HTTPHandler handler = (req, res) -> {
println("Handling ... (slowly)");
// Note that if you comment out the sleep, the test should fail.
sleep(3_000L);
res.setStatus(200);
var body = "I'm slow but I'm good.".getBytes(StandardCharsets.UTF_8);
res.setContentLength(body.length);
res.getOutputStream().write(body);
res.getOutputStream().close();
println("Closed");
};
var instrumenter = new CountingInstrumenter();
try (var ignore = makeServer("http", handler, instrumenter)
// The processing timeout should be triggered due to how slow the server is to write back to the client.
// - Increase other timeouts to be certain we are testing the correct one.
.withProcessingTimeoutDuration(Duration.ofSeconds(1))
.withInitialReadTimeout(Duration.ofSeconds(30))
.withKeepAliveTimeoutDuration(Duration.ofSeconds(30))
.start();
// Open a socket to the server and begin writing.
Socket socket = new Socket("127.0.0.1", 4242)) {
var out = socket.getOutputStream();
// 1. Write a body to the server
out.write("""
GET / HTTP/1.1\r
Host: localhost:42\r
Connection: close\r
Content-Length: 4\r
\r
body
""".getBytes());
out.flush();
// 2. Read from the server, assuming you will receive a response.
// However, the server is very slow, it is going to wait 4s before it writes the response.
// - We have configured the server with a 1s read timeout.
// - Expect that we will have received a SocketTimeoutException and as such the response will be empty.
var response = socket.getInputStream().readAllBytes();
assertEquals(response.length, 0, new String(response));
assertEquals(instrumenter.getClosedConnections(), 1);
}
}
@Test(dataProvider = "schemesAndResponseBufferSizes")
public void simpleGet(String scheme, int responseBufferSize) throws Exception {
HTTPHandler handler = (req, res) -> {
assertEquals(req.getAcceptEncodings(), List.of("deflate", "compress", "identity", "gzip", "br"));
assertEquals(req.getBaseURL(), scheme.equals("http") ? "http://localhost:4242" : "https://local.fusionauth.io:4242");
assertEquals(req.getContentType(), "text/plain");
assertEquals(req.getCharacterEncoding(), StandardCharsets.ISO_8859_1);
assertEquals(req.getHeader(Headers.Origin), "https://example.com");
assertEquals(req.getHeader(Headers.Referer), "foobar.com");
assertEquals(req.getHeader(Headers.UserAgent), "java-http test");
assertEquals(req.getHost(), scheme.equals("http") ? "localhost" : "local.fusionauth.io");
assertEquals(req.getIPAddress(), "127.0.0.1");
assertEquals(req.getLocales(), List.of(Locale.ENGLISH, Locale.GERMAN, Locale.FRENCH));
assertEquals(req.getMethod(), HTTPMethod.GET);
assertEquals(req.getParameter("foo "), "bar ");
assertEquals(req.getPath(), "/api/system/version");
assertEquals(req.getPort(), 4242);
assertEquals(req.getProtocol(), "HTTP/1.1");
assertEquals(req.getQueryString(), "foo%20=bar%20");
assertEquals(req.getScheme(), scheme);
assertEquals(req.getURLParameter("foo "), "bar ");
res.setHeader(Headers.ContentType, "text/plain");
// Compression is on by default, don't write a Content-Length header it will be wrong.
res.setStatus(200);
try {
OutputStream outputStream = res.getOutputStream();
outputStream.write(ExpectedResponse.getBytes());
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
};
try (var client = makeClient(scheme, null);
var ignore = makeServer(scheme, handler).withResponseBufferSize(responseBufferSize).start()) {
URI uri = makeURI(scheme, "?foo%20=bar%20");
HttpRequest request = HttpRequest.newBuilder()
.uri(uri)
.header(Headers.AcceptEncoding, "deflate, compress, br;q=0.5, gzip;q=0.8, identity;q=1.0")
.header(Headers.AcceptLanguage, "en, fr;q=0.7, de;q=0.8")
.header(Headers.ContentType, "text/plain; charset=ISO-8859-1")
.header(Headers.Origin, "https://example.com")
.header(Headers.Referer, "foobar.com")
.header(Headers.UserAgent, "java-http test")
.GET()
.build();
var response = client.send(request, r -> BodySubscribers.ofInputStream());
assertEquals(response.statusCode(), 200);
assertEquals(response.headers().firstValue(Headers.ContentEncoding).get(), "deflate");
assertEquals(response.headers().firstValue(Headers.TransferEncoding).get(), "chunked");
var result = new String(new InflaterInputStream(response.body()).readAllBytes(), StandardCharsets.UTF_8);
assertEquals(result, ExpectedResponse);
}
}
@Test
public void simpleGetMultiplePorts() throws Exception {
HTTPHandler handler = (req, res) -> {
res.setHeader(Headers.ContentType, "text/plain");
res.setHeader(Headers.ContentLength, "16");
res.setStatus(200);
try {
OutputStream outputStream = res.getOutputStream();
outputStream.write(ExpectedResponse.getBytes());
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
};
var certChain = new Certificate[]{certificate, intermediateCertificate};
try (var client = makeClient("https", null);
var ignore = new HTTPServer().withHandler(handler)
.withListener(new HTTPListenerConfiguration(4242))
.withListener(new HTTPListenerConfiguration(4243))
.withListener(new HTTPListenerConfiguration(4244, certChain, keyPair.getPrivate()))
.withLoggerFactory(AccumulatingLoggerFactory.FACTORY)
.start()) {
URI uri = URI.create("http://localhost:4242/api/system/version?foo=bar");
HttpRequest request = HttpRequest.newBuilder().uri(uri).GET().build();
var response = client.send(request, r -> BodySubscribers.ofString(StandardCharsets.UTF_8));
assertEquals(response.statusCode(), 200);
assertEquals(response.body(), ExpectedResponse);
// Try the other port
uri = URI.create("http://localhost:4243/api/system/version?foo=bar");
request = HttpRequest.newBuilder().uri(uri).GET().build();
response = client.send(request, r -> BodySubscribers.ofString(StandardCharsets.UTF_8));
assertEquals(response.statusCode(), 200);
assertEquals(response.body(), ExpectedResponse);
// Try the TLS port
uri = URI.create("https://local.fusionauth.io:4244/api/system/version?foo=bar");
request = HttpRequest.newBuilder().uri(uri).GET().build();
response = client.send(request, r -> BodySubscribers.ofString(StandardCharsets.UTF_8));
assertEquals(response.statusCode(), 200);
assertEquals(response.body(), ExpectedResponse);
}
}
@Test(dataProvider = "schemesAndResponseBufferSizes")
public void simplePost(String scheme, int responseBufferSize) throws Exception {
HTTPHandler handler = (req, res) -> {
println("Handling");
assertEquals(req.getHeader(Headers.ContentType), "application/json"); // Mixed case
try {
println("Reading");
byte[] body = req.getInputStream().readAllBytes();
assertEquals(new String(body), RequestBody);
} catch (IOException e) {
fail("Unable to parse body", e);
}
println("Done");
res.setHeader(Headers.ContentType, "text/plain");
res.setHeader(Headers.ContentLength, "16");
res.setStatus(200);
try {
println("Writing");
OutputStream outputStream = res.getOutputStream();
outputStream.write(ExpectedResponse.getBytes());
outputStream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
};
try (var ignore = makeServer(scheme, handler).withResponseBufferSize(responseBufferSize).start(); var client = makeClient(scheme, null)) {
URI uri = makeURI(scheme, "?foo=bar");
var response = client.send(
HttpRequest.newBuilder().uri(uri).header(Headers.ContentType, "application/json").POST(BodyPublishers.ofString(RequestBody)).build(),
r -> BodySubscribers.ofString(StandardCharsets.UTF_8)
);
assertEquals(response.statusCode(), 200);
assertEquals(response.body(), ExpectedResponse);
}
}
@Test(dataProvider = "schemes", groups = "timeouts")
public void slowClient(String scheme) throws Exception {
// Test a slow connection where the HTTP server is blocked because we cannot write to the output stream as fast as we'd like. The
// default buffer on macOS seems to be 768k (from my testing). I set this to 8MB which should hopefully cause the writes to back up.
// - This tests the minimumWriteThroughput and writeThroughputCalculationDelayDuration
byte[] bytes = new byte[1024 * 1024 * 8];
new SecureRandom().nextBytes(bytes);
// For debug
boolean debug = false;
// The server will write this back to the client
HTTPHandler handler = (req, res) -> {
res.setContentType("application/octet-stream");
res.setContentLength(bytes.length);
res.setStatus(200);
int iteration = 1;
var out = res.getOutputStream();
for (int i = 0; i < bytes.length; i += 1024 * 8) {
out.write(bytes, i, 1024 * 8);
//noinspection ConstantValue
if (debug) {
println("> Wrote [" + (8 * 1024) + "] bytes. Total bytes written [" + (iteration++ * 8 * 1024) + "].");
}
}
out.close();
};
// Set the min write throughput to 1 Megabit / second
// - In this case the server is trying to write the client, and it cannot do so fast enough.
AtomicBoolean slept = new AtomicBoolean(false);
AtomicInteger totalBytesReceived = new AtomicInteger();
try (var client = makeClient(scheme, null); var ignore = makeServer(scheme, handler)
// By default, we will wait 5 seconds before we calculate write throughput, reduce to this to 1 ms for this test.
.withWriteThroughputCalculationDelayDuration(Duration.ofMillis(1))
.withMinimumWriteThroughput(1024 * 1024 * 1024).start()) {
URI uri = makeURI(scheme, "");
client.send(
// Don't keep this connection open because the timeouts aren't the same on a keep alive.
HttpRequest.newBuilder().uri(uri).GET().build(),
r -> BodySubscribers.ofByteArrayConsumer(optional -> {
byte[] actual = optional.orElse(null);
if (actual != null) {
// Sleep once since the server should fail after the first batch, but since Java or the OS might cache a lot of bytes it
// read from the socket, we can't sleep for too long, otherwise, this test will never complete
if (!slept.get()) {
int sleep = 5_000;
//noinspection ConstantValue
if (debug) {
println("> Received [" + actual.length + "] bytes. Total bytes received [" + totalBytesReceived.addAndGet(actual.length) + "]. Sleep [" + sleep + "] ms.");
}
sleep(sleep); // We expect to only wait for 2,000 ms until the cleaner runs to find slow clients.
slept.set(true);
}
} else {
println("no bytes");
}
})
);