|
2 | 2 |
|
3 | 3 | import com.google.api.client.http.HttpContent;
|
4 | 4 | import com.google.api.client.http.InputStreamContent;
|
| 5 | +import com.google.api.client.http.LowLevelHttpResponse; |
5 | 6 | import com.google.api.client.http.javanet.NetHttpRequest.OutputWriter;
|
6 | 7 | import com.google.api.client.testing.http.HttpTesting;
|
7 | 8 | import com.google.api.client.testing.http.javanet.MockHttpURLConnection;
|
@@ -82,4 +83,118 @@ private static void postWithTimeout(int timeout) throws Exception {
|
82 | 83 | request.execute(new SleepingOutputWriter(5000L));
|
83 | 84 | }
|
84 | 85 |
|
| 86 | + @Test |
| 87 | + public void testInterruptedWriteWithResponse() throws Exception { |
| 88 | + MockHttpURLConnection connection = new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL)) { |
| 89 | + @Override |
| 90 | + public OutputStream getOutputStream() throws IOException { |
| 91 | + return new OutputStream() { |
| 92 | + @Override |
| 93 | + public void write(int b) throws IOException { |
| 94 | + throw new IOException("Error writing request body to server"); |
| 95 | + } |
| 96 | + }; |
| 97 | + } |
| 98 | + }; |
| 99 | + connection.setResponseCode(401); |
| 100 | + connection.setRequestMethod("POST"); |
| 101 | + NetHttpRequest request = new NetHttpRequest(connection); |
| 102 | + InputStream is = NetHttpRequestTest.class.getClassLoader().getResourceAsStream("file.txt"); |
| 103 | + HttpContent content = new InputStreamContent("text/plain", is); |
| 104 | + request.setStreamingContent(content); |
| 105 | + |
| 106 | + LowLevelHttpResponse response = request.execute(); |
| 107 | + assertEquals(401, response.getStatusCode()); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testInterruptedWriteWithoutResponse() throws Exception { |
| 112 | + MockHttpURLConnection connection = new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL)) { |
| 113 | + @Override |
| 114 | + public OutputStream getOutputStream() throws IOException { |
| 115 | + return new OutputStream() { |
| 116 | + @Override |
| 117 | + public void write(int b) throws IOException { |
| 118 | + throw new IOException("Error writing request body to server"); |
| 119 | + } |
| 120 | + }; |
| 121 | + } |
| 122 | + }; |
| 123 | + connection.setRequestMethod("POST"); |
| 124 | + NetHttpRequest request = new NetHttpRequest(connection); |
| 125 | + InputStream is = NetHttpRequestTest.class.getClassLoader().getResourceAsStream("file.txt"); |
| 126 | + HttpContent content = new InputStreamContent("text/plain", is); |
| 127 | + request.setStreamingContent(content); |
| 128 | + |
| 129 | + try { |
| 130 | + request.execute(); |
| 131 | + fail("Expected to throw an IOException"); |
| 132 | + } catch (IOException e) { |
| 133 | + assertEquals("Error writing request body to server", e.getMessage()); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testInterruptedWriteErrorOnResponse() throws Exception { |
| 139 | + MockHttpURLConnection connection = new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL)) { |
| 140 | + @Override |
| 141 | + public OutputStream getOutputStream() throws IOException { |
| 142 | + return new OutputStream() { |
| 143 | + @Override |
| 144 | + public void write(int b) throws IOException { |
| 145 | + throw new IOException("Error writing request body to server"); |
| 146 | + } |
| 147 | + }; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public int getResponseCode() throws IOException { |
| 152 | + throw new IOException("Error parsing response code"); |
| 153 | + } |
| 154 | + }; |
| 155 | + connection.setRequestMethod("POST"); |
| 156 | + NetHttpRequest request = new NetHttpRequest(connection); |
| 157 | + InputStream is = NetHttpRequestTest.class.getClassLoader().getResourceAsStream("file.txt"); |
| 158 | + HttpContent content = new InputStreamContent("text/plain", is); |
| 159 | + request.setStreamingContent(content); |
| 160 | + |
| 161 | + try { |
| 162 | + request.execute(); |
| 163 | + fail("Expected to throw an IOException"); |
| 164 | + } catch (IOException e) { |
| 165 | + assertEquals("Error writing request body to server", e.getMessage()); |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void testErrorOnClose() throws Exception { |
| 171 | + MockHttpURLConnection connection = new MockHttpURLConnection(new URL(HttpTesting.SIMPLE_URL)) { |
| 172 | + @Override |
| 173 | + public OutputStream getOutputStream() throws IOException { |
| 174 | + return new OutputStream() { |
| 175 | + @Override |
| 176 | + public void write(int b) throws IOException { |
| 177 | + return; |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void close() throws IOException { |
| 182 | + throw new IOException("Error during close"); |
| 183 | + } |
| 184 | + }; |
| 185 | + } |
| 186 | + }; |
| 187 | + connection.setRequestMethod("POST"); |
| 188 | + NetHttpRequest request = new NetHttpRequest(connection); |
| 189 | + InputStream is = NetHttpRequestTest.class.getClassLoader().getResourceAsStream("file.txt"); |
| 190 | + HttpContent content = new InputStreamContent("text/plain", is); |
| 191 | + request.setStreamingContent(content); |
| 192 | + |
| 193 | + try { |
| 194 | + request.execute(); |
| 195 | + fail("Expected to throw an IOException"); |
| 196 | + } catch (IOException e) { |
| 197 | + assertEquals("Error during close", e.getMessage()); |
| 198 | + } |
| 199 | + } |
85 | 200 | }
|
0 commit comments