Skip to content

Commit

Permalink
#620 RqBytes clean up, RqMultipartTest works
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed Feb 22, 2016
1 parent 55103f0 commit edbf31b
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 96 deletions.
5 changes: 4 additions & 1 deletion src/main/java/org/takes/rq/RqFake.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ public RqFake(final List<String> head, final CharSequence body) {
* @param body Body
*/
public RqFake(final List<String> head, final byte[] body) {
this(head, new ByteArrayInputStream(body));
this(
head,
new ByteArrayInputStream(Arrays.copyOf(body, body.length))
);
}

/**
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/org/takes/rq/RqHeaders.java
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public List<String> header(final CharSequence key)
);
final List<String> list;
if (values == null) {
list = new VerboseList<String>(
list = new VerboseList<>(
Collections.<String>emptyList(),
String.format(
// @checkstyle LineLengthCheck (1 line)
Expand All @@ -108,7 +108,7 @@ public List<String> header(final CharSequence key)
)
);
} else {
list = new VerboseList<String>(
list = new VerboseList<>(
values,
String.format(
// @checkstyle LineLengthCheck (1 line)
Expand Down Expand Up @@ -140,8 +140,7 @@ private Map<String, List<String>> map() throws IOException {
);
}
head.next();
final Map<String, List<String>> map =
new HashMap<String, List<String>>(0);
final Map<String, List<String>> map = new HashMap<>(0);
while (head.hasNext()) {
final String line = head.next();
final String[] parts = line.split(":", 2);
Expand Down Expand Up @@ -211,7 +210,8 @@ public String single(final CharSequence name) throws IOException {
throw new HttpException(
HttpURLConnection.HTTP_BAD_REQUEST,
String.format(
"header \"%s\" is mandatory", name
"header \"%s\" is mandatory, not found among %s",
name, this.names()
)
);
}
Expand Down
83 changes: 40 additions & 43 deletions src/main/java/org/takes/rq/RqMultipart.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,9 @@ public interface RqMultipart extends Request {
* @author Yegor Bugayenko (yegor@teamed.io)
* @version $Id$
* @since 0.9
* @see <a href="http://www.w3.org/TR/html401/interact/forms.html">
* Forms in HTML</a>
* @see <a href="http://www.w3.org/TR/html401/interact/forms.html">Forms in HTML</a>
* @checkstyle ClassDataAbstractionCouplingCheck (500 lines)
* @see org.takes.rq.RqGreedy
* @see RqGreedy
*/
@EqualsAndHashCode(callSuper = true)
final class Base extends RqWrap implements RqMultipart {
Expand Down Expand Up @@ -148,7 +147,7 @@ public Base(final Request req) throws IOException {
// @checkstyle MagicNumberCheck (1 line)
Math.min(8192, stream.available())
);
this.map = this.buildRequests(req);
this.map = this.requests(req);
} finally {
this.body.close();
}
Expand All @@ -162,15 +161,15 @@ public Iterable<Request> part(final CharSequence name) {
.get(name.toString().toLowerCase(Locale.ENGLISH));
final Iterable<Request> iter;
if (values == null) {
iter = new VerboseIterable<Request>(
iter = new VerboseIterable<>(
Collections.<Request>emptyList(),
new Sprintf(
"there are no parts by name \"%s\" among %d others: %s",
name, this.map.size(), this.map.keySet()
)
);
} else {
iter = new VerboseIterable<Request>(
iter = new VerboseIterable<>(
values,
new Sprintf(
"there are just %d parts by name \"%s\"",
Expand All @@ -190,7 +189,7 @@ public Iterable<String> names() {
* @return The requests map that use the part name as a map key
* @throws IOException If fails
*/
private Map<String, List<Request>> buildRequests(
private Map<String, List<Request>> requests(
final Request req) throws IOException {
final String header = new RqHeaders.Smart(
new RqHeaders.Base(req)
Expand Down Expand Up @@ -269,7 +268,7 @@ private Request make(final byte[] boundary) throws IOException {
);
// @checkstyle MultipleStringLiteralsCheck (1 line)
channel.write(
ByteBuffer.wrap(Fake.CRLF.getBytes(StandardCharsets.UTF_8))
ByteBuffer.wrap(RqMultipart.Fake.CRLF.getBytes(StandardCharsets.UTF_8))
);
this.copy(channel, boundary);
} finally {
Expand Down Expand Up @@ -442,8 +441,9 @@ final class Fake implements RqMultipart {
public Fake(final Request req, final Request... dispositions)
throws IOException {
this.fake = new RqMultipart.Base(
//@checkstyle AnonInnerLength (1 line)
new FakeMultipartRequest(req, dispositions)
new RqMultipart.Fake.FakeMultipartRequest(
req, dispositions
)
);
}
@Override
Expand All @@ -464,95 +464,92 @@ public InputStream body() throws IOException {
}
/**
* Fake body stream creator.
* @param dispositions Fake request body parts
* @param parts Fake request body parts
* @return InputStream of given dispositions
* @throws IOException If fails
*/
private static InputStream fakeStream(final Request... dispositions)
private static InputStream fakeStream(final Request... parts)
throws IOException {
final StringBuilder builder = fakeBody(dispositions);
return new ByteArrayInputStream(
builder.toString().getBytes(StandardCharsets.UTF_8)
RqMultipart.Fake.fakeBody(parts).toString().getBytes(
StandardCharsets.UTF_8
)
);
}

/**
* Fake body creator.
* @param dispositions Fake request body parts
* @param parts Fake request body parts
* @return StringBuilder of given dispositions
* @throws IOException If fails
*/
@SuppressWarnings("PMD.InsufficientStringBufferDeclaration")
private static StringBuilder fakeBody(final Request... dispositions)
private static StringBuilder fakeBody(final Request... parts)
throws IOException {
final StringBuilder builder = new StringBuilder();
for (final Request each : dispositions) {
builder.append(String.format("--%s", Fake.BOUNDARY))
.append(Fake.CRLF)
for (final Request part : parts) {
builder.append(String.format("--%s", RqMultipart.Fake.BOUNDARY))
.append(RqMultipart.Fake.CRLF)
// @checkstyle MultipleStringLiteralsCheck (1 line)
.append("Content-Disposition: ")
.append(
new RqHeaders.Smart(
new RqHeaders.Base(each)
new RqHeaders.Base(part)
// @checkstyle MultipleStringLiteralsCheck (1 line)
).single("Content-Disposition")
).append(Fake.CRLF);
final String body = new RqPrint(each).printBody();
if (!(Fake.CRLF.equals(body) || "".equals(body))) {
builder.append(Fake.CRLF).append(body).append(Fake.CRLF);
).append(RqMultipart.Fake.CRLF);
final String body = new RqPrint(part).printBody();
if (!(RqMultipart.Fake.CRLF.equals(body) || body.isEmpty())) {
builder.append(RqMultipart.Fake.CRLF)
.append(body)
.append(RqMultipart.Fake.CRLF);
}
}
builder.append("Content-Transfer-Encoding: utf-8").append(Fake.CRLF)
.append(String.format("--%s--", Fake.BOUNDARY));
builder.append("Content-Transfer-Encoding: utf-8")
.append(RqMultipart.Fake.CRLF)
.append(String.format("--%s--", RqMultipart.Fake.BOUNDARY));
return builder;
}

/**
* This class is using a decorator pattern for representing
* a fake HTTP multipart request.
*/
private static class FakeMultipartRequest implements Request {

private static final class FakeMultipartRequest implements Request {
/**
* Request object. Holds a value for the header.
*/
private final Request req;

/**
* Holding multiple request body parts.
*/
private final Request[] dispositions;

private final Request[] parts;
/**
* The Constructor for the class.
* @param req The Request object
* @param dispositions The sequence of dispositions
* @param rqst The Request object
* @param list The sequence of dispositions
*/
FakeMultipartRequest(
final Request req, final Request... dispositions
) {
this.req = req;
this.dispositions = dispositions;
FakeMultipartRequest(final Request rqst, final Request... list) {
this.req = rqst;
this.parts = list;
}

@Override
public Iterable<String> head() throws IOException {
return new RqWithHeaders(
this.req,
String.format(
"Content-Type: multipart/form-data; boundary=%s",
Fake.BOUNDARY
RqMultipart.Fake.BOUNDARY
),
String.format(
"Content-Length: %s",
Fake.fakeBody(this.dispositions).length()
RqMultipart.Fake.fakeBody(this.parts).length()
)
).head();
}

@Override
public InputStream body() throws IOException {
return Fake.fakeStream(this.dispositions);
return RqMultipart.Fake.fakeStream(this.parts);
}
}
}
Expand Down
Loading

0 comments on commit edbf31b

Please sign in to comment.