Skip to content

Commit

Permalink
feat: In requestIPFSTests, add testAddWithBytesBody and testAddWithSt…
Browse files Browse the repository at this point in the history
…reamBody.

Signed-off-by: jefft0 <[email protected]>
  • Loading branch information
jefft0 committed Jan 9, 2023
1 parent ca85d0d commit 7e81ccc
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
import org.junit.Test;
import org.junit.rules.Timeout;
import org.junit.runner.RunWith;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.Arrays;

import static org.junit.Assert.*;
Expand All @@ -34,6 +36,15 @@ public class requestIPFSTests {
(byte)0x70, (byte)0xf0, (byte)0x2b, (byte)0xeb, (byte)0xe9, (byte)0x77, (byte)0xfe, (byte)0x89,
(byte)0xef, (byte)0x67, (byte)0x51, (byte)0x2f, (byte)0x98, (byte)0x82, (byte)0xd8, (byte)0x60
};
// The boundary for a multipart message is a unique string. See https://en.wikipedia.org/wiki/MIME#Multipart_messages
private static String boundary = "------------------------f33e457ed9f80969";
private byte[] addRequestBody =
("--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"file\"\r\n" +
"Content-Type: application/octet-stream\r\n\r\n" +
"hello" +
"\r\n--" + boundary + "--\r\n").getBytes();
private String addRequestExpectedHash = "QmWfVY9y3xjsixTgbd9AorQxH7VtMpzfx2HaWtsoUYecaX";

@Rule
public Timeout globalTimeout = Timeout.seconds(600);
Expand Down Expand Up @@ -118,4 +129,30 @@ public void testCatFileStream() throws Exception {
);
}
}

@Test
public void testAddWithBytesBody() throws Exception {
ArrayList<JSONObject> response = ipfs.newRequest("add")
.withHeader("Content-Type",
"multipart/form-data; boundary=" + boundary)
.withBody(addRequestBody)
.sendToJSONList();

assertEquals("Added file should have the correct CID",
addRequestExpectedHash,
response.get(0).getString("Hash"));
}

@Test
public void testAddWithStreamBody() throws Exception {
ArrayList<JSONObject> response = ipfs.newRequest("add")
.withHeader("Content-Type",
"multipart/form-data; boundary=" + boundary)
.withBody(new ByteArrayInputStream(addRequestBody))
.sendToJSONList();

assertEquals("Added file should have the correct CID",
addRequestExpectedHash,
response.get(0).getString("Hash"));
}
}
51 changes: 51 additions & 0 deletions ios/Bridge/GomobileIPFSTests/RequestIPFSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ class RequestIPFSTests: XCTestCase {
private var expectedFileLength: Int = 2940
private var expectedFileSha256: String =
"SHA256 digest: b7042c3f6efc09d29dee4566dec6e34270f02bebe977fe89ef67512f9882d860"
// The boundary for a multipart message is a unique string. See https://en.wikipedia.org/wiki/MIME#Multipart_messages
private static var boundary : String = "------------------------f33e457ed9f80969"
private var addRequestBody : Data =
("--" + boundary + "\r\n" +
"Content-Disposition: form-data; name=\"file\"\r\n" +
"Content-Type: application/octet-stream\r\n\r\n" +
"hello" +
"\r\n--" + boundary + "--\r\n").data(using: .utf8)!;
private var addRequestExpectedHash: String = "QmWfVY9y3xjsixTgbd9AorQxH7VtMpzfx2HaWtsoUYecaX";

override func setUp() {
do {
Expand Down Expand Up @@ -113,4 +122,46 @@ class RequestIPFSTests: XCTestCase {
"response should have the correct SHA256"
)
}

func testAddWithBytesBody() throws {
guard let response = try ipfs.newRequest("add")
.with(header: "Content-Type",
value: "multipart/form-data; boundary=" + RequestIPFSTests.boundary)
.with(body: RequestBody.bytes(addRequestBody))
.sendToDict() else {
XCTFail("error while casting dict for \"add\"")
return
}
guard let hash = response["Hash"] as? String else {
XCTFail("error while casting value associated to \"Hash\" key")
return
}

XCTAssertEqual(
addRequestExpectedHash,
hash,
"Added file should have the correct CID"
)
}

func testAddWithStreamBody() throws {
guard let response = try ipfs.newRequest("add")
.with(header: "Content-Type",
value: "multipart/form-data; boundary=" + RequestIPFSTests.boundary)
.with(body: RequestBody.stream(InputStream(data: addRequestBody)))
.sendToDict() else {
XCTFail("error while casting dict for \"add\"")
return
}
guard let hash = response["Hash"] as? String else {
XCTFail("error while casting value associated to \"Hash\" key")
return
}

XCTAssertEqual(
addRequestExpectedHash,
hash,
"Added file should have the correct CID"
)
}
}

0 comments on commit 7e81ccc

Please sign in to comment.