|
| 1 | +package com.blazemeter.jmeter.http2.core; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertSame; |
| 4 | + |
| 5 | +import com.blazemeter.jmeter.http2.HTTP2TestBase; |
| 6 | +import com.blazemeter.jmeter.http2.sampler.HTTP2Sampler; |
| 7 | +import com.blazemeter.jmeter.http2.sampler.JMeterTestUtils; |
| 8 | +import java.lang.reflect.Field; |
| 9 | +import java.lang.reflect.Method; |
| 10 | +import java.net.URI; |
| 11 | +import org.apache.jmeter.protocol.http.sampler.HTTPSampleResult; |
| 12 | +import org.apache.jmeter.protocol.http.util.HTTPConstants; |
| 13 | +import org.junit.Before; |
| 14 | +import org.junit.BeforeClass; |
| 15 | +import org.junit.Test; |
| 16 | + |
| 17 | +/** |
| 18 | + * Pins which client a bodied cleartext request gets. |
| 19 | + * |
| 20 | + * <p>Such requests are diverted to the HTTP/1.1-only client to sidestep the h2c Upgrade dance, |
| 21 | + * whose first request travels as plain HTTP/1.1 and which servers handle inconsistently when it |
| 22 | + * carries a body. That is a shortcut around a negotiation, not a protocol choice, so it must not |
| 23 | + * survive where HTTP/1.1 is disabled or where h2c is spoken from the first byte anyway — issue |
| 24 | + * #157, where POSTs to an h2c-only endpoint went out as HTTP/1.1 and the server's HTTP/2 frames |
| 25 | + * came back through the HTTP/1.1 parser as {@code Illegal character CNTL=0x0}. |
| 26 | + */ |
| 27 | +public class CleartextBodyClientSelectionTest extends HTTP2TestBase { |
| 28 | + |
| 29 | + private static final URI CLEARTEXT_URI = URI.create("http://h2c-only.example.com:8000/policies"); |
| 30 | + |
| 31 | + private HTTP2JettyClient client; |
| 32 | + |
| 33 | + @BeforeClass |
| 34 | + public static void setupClass() { |
| 35 | + JMeterTestUtils.setupJmeterEnv(); |
| 36 | + } |
| 37 | + |
| 38 | + @Before |
| 39 | + public void setUp() throws Exception { |
| 40 | + HTTP2JettyClient.clearStaticProtocolCaches(); |
| 41 | + client = new HTTP2JettyClient(); |
| 42 | + setBoolean("enableHttp1", true); |
| 43 | + setBoolean("enableHttp2", true); |
| 44 | + setBoolean("enableHttp3", false); |
| 45 | + setBoolean("http1UpgradeRequired", false); |
| 46 | + setBoolean("http2PriorKnowledgeEnabled", false); |
| 47 | + setBoolean("http3PriorKnowledgeEnabled", false); |
| 48 | + setBoolean("h2cCacheEnabled", false); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void shouldUseHttp1OnlyForCleartextPostWhenUpgradeIsTheAlternative() throws Exception { |
| 53 | + setBoolean("http1UpgradeRequired", true); |
| 54 | + |
| 55 | + assertSame("a bodied cleartext POST must skip the h2c Upgrade dance", |
| 56 | + field("httpClientHttp1Only"), resolveClientForRequest(post())); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void shouldNotUseHttp1OnlyForCleartextPostWhenHttp1IsDisabled() throws Exception { |
| 61 | + setBoolean("enableHttp1", false); |
| 62 | + setBoolean("http1UpgradeRequired", true); |
| 63 | + |
| 64 | + assertSame("with HTTP/1.1 disabled a bodied POST must still be spoken as h2c", |
| 65 | + field("httpClientH2cPrior"), resolveClientForRequest(post())); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void shouldNotUseHttp1OnlyForCleartextPostWithH2cPriorKnowledge() throws Exception { |
| 70 | + setBoolean("http2PriorKnowledgeEnabled", true); |
| 71 | + |
| 72 | + assertSame("prior knowledge means there is no Upgrade to avoid", |
| 73 | + field("httpClientH2cPrior"), resolveClientForRequest(post())); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void shouldUseHttp1OnlyForCleartextGetSendingParametersAsBody() throws Exception { |
| 78 | + setBoolean("http1UpgradeRequired", true); |
| 79 | + HTTP2Sampler sampler = sampler(HTTPConstants.GET); |
| 80 | + sampler.setPostBodyRaw(true); |
| 81 | + |
| 82 | + assertSame(field("httpClientHttp1Only"), |
| 83 | + resolveClientForRequest(sampler, result(HTTPConstants.GET))); |
| 84 | + } |
| 85 | + |
| 86 | + private HTTP2Sampler post() { |
| 87 | + HTTP2Sampler sampler = sampler(HTTPConstants.POST); |
| 88 | + sampler.addArgument("policy", "value"); |
| 89 | + return sampler; |
| 90 | + } |
| 91 | + |
| 92 | + private HTTP2Sampler sampler(String method) { |
| 93 | + HTTP2Sampler sampler = new HTTP2Sampler(); |
| 94 | + sampler.setMethod(method); |
| 95 | + return sampler; |
| 96 | + } |
| 97 | + |
| 98 | + private HTTPSampleResult result(String method) throws Exception { |
| 99 | + HTTPSampleResult result = new HTTPSampleResult(); |
| 100 | + result.setURL(CLEARTEXT_URI.toURL()); |
| 101 | + result.setHTTPMethod(method); |
| 102 | + return result; |
| 103 | + } |
| 104 | + |
| 105 | + private Object resolveClientForRequest(HTTP2Sampler sampler) throws Exception { |
| 106 | + return resolveClientForRequest(sampler, result(HTTPConstants.POST)); |
| 107 | + } |
| 108 | + |
| 109 | + private Object resolveClientForRequest(HTTP2Sampler sampler, HTTPSampleResult result) |
| 110 | + throws Exception { |
| 111 | + Method m = HTTP2JettyClient.class.getDeclaredMethod( |
| 112 | + "resolveClientForRequest", HTTP2Sampler.class, HTTPSampleResult.class); |
| 113 | + m.setAccessible(true); |
| 114 | + return m.invoke(client, sampler, result); |
| 115 | + } |
| 116 | + |
| 117 | + private Object field(String name) throws Exception { |
| 118 | + Field f = HTTP2JettyClient.class.getDeclaredField(name); |
| 119 | + f.setAccessible(true); |
| 120 | + return f.get(client); |
| 121 | + } |
| 122 | + |
| 123 | + private void setBoolean(String name, boolean value) throws Exception { |
| 124 | + Field f = HTTP2JettyClient.class.getDeclaredField(name); |
| 125 | + f.setAccessible(true); |
| 126 | + f.setBoolean(client, value); |
| 127 | + } |
| 128 | + |
| 129 | +} |
0 commit comments