Skip to content

Commit

Permalink
Changes proposed by Kamesh
Browse files Browse the repository at this point in the history
Signed-off-by: Erlend Valle <[email protected]>
  • Loading branch information
erlendv committed Aug 3, 2019
1 parent 8b462b7 commit 789775d
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void handle(HttpExchange t) throws IOException {

OutputStream os = t.getResponseBody();
os.write(bytesOut);
os.close();
t.close();

System.out.println("Request / " + Integer.toString(bytesOut.length) +" bytes written.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@

package com.openfaas.model;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.HashMap;
import java.net.URLDecoder;
import java.io.UnsupportedEncodingException;


public class Request implements IRequest {
Expand All @@ -23,13 +20,13 @@ public class Request implements IRequest {
private Map<String, String> path;
private InputStream inputStream;

public Request(Map<String, String> headers) {
this.headers = headers;
}
public Request(String body, Map<String, String> headers) {
this(body, headers, "", "");
}

public Request(String body, Map<String, String> headers, String queryRaw, String path) {
this(headers, queryRaw, path, null);
this.body = body;
this(headers, queryRaw, path, new ByteArrayInputStream(body != null ? body.getBytes(StandardCharsets.UTF_8) : new byte[0]));

}

public Request(Map<String, String> headers, String queryRaw, String path, InputStream inputStream) {
Expand Down
37 changes: 35 additions & 2 deletions template/java8/model/src/test/java/RequestTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class RequestTest {
@Test
public void testSingleRequestParameterGetSet()
{
Request request = new Request("", null, "testParam=testParamValue", null);
Request request = new Request((String)null, null, "testParam=testParamValue", null);
assertEquals("testParamValue", request.getQuery().get("testParam"));
}

Expand Down Expand Up @@ -131,7 +131,7 @@ public void testParseParametersWithOddParameters()
}

@Test
public void testRequestGetBodyWorksAsBefore() {
public void testRequestGetBodyWithString() {
Request request = new Request(null, null, null, new ByteArrayInputStream("Øl får meg glad".getBytes()));
assertEquals("Øl får meg glad", request.getBody());
}
Expand All @@ -149,8 +149,41 @@ public void testRequestInputStream() {
assertTrue(Arrays.equals(originalData, requestBodyData));
} catch (IOException e) {
throw new AssertionError(e);
} catch (AssertionError e) {
System.out.println(e.getMessage());
throw e;
}
}

@Test
public void testAlternateSetBodyGetBodyData() {
try {
String requestBody = "Øl får meg glad";
byte[] originalData = requestBody.getBytes();
Request request = new Request(requestBody, null, null, null);
byte[] buffer = new byte[64];
int read = request.getInputStream().read(buffer);
byte[] requestBodyData = new byte[read];
System.arraycopy(buffer, 0, requestBodyData, 0, read);
assertTrue(Arrays.equals(originalData, requestBodyData));
} catch (IOException e) {
throw new AssertionError(e);
} catch (AssertionError e) {
System.out.println(e.getMessage());
throw e;
}
}

@Test
public void testAlternateSetBodyDataGetBody() {
try {
String originalRequestBody = "Øl får meg glad";
Request request = new Request(null, null, null, new ByteArrayInputStream(originalRequestBody.getBytes()));
String requestBody = request.getBody();
assertEquals(originalRequestBody, requestBody);
} catch (AssertionError e) {
System.out.println(e.getMessage());
throw e;
}
}
}

0 comments on commit 789775d

Please sign in to comment.