diff --git a/integration-test/end-to-end.js b/integration-test/end-to-end.js index b419ece..cf12e58 100644 --- a/integration-test/end-to-end.js +++ b/integration-test/end-to-end.js @@ -77,6 +77,82 @@ const testUpdateMessage = async (channel, token) => { return result; }; +const testUploadFile = async (channel, token) => { + // WIP!!! + // 1. Get this test to work + // 2. Move the code to the src/ section + // 3. Add integration test + + return new Promise((resolve, reject) => { + const https = require("https"); + const fs = require("fs"); + + let options = { + method: "POST", + hostname: "slack.com", + path: "/api/files.upload", + headers: { + Authorization: "Bearer " + token, + }, + }; + + const req = https.request(options, (res) => { + let chunks = []; + + res.on("data", (chunk) => { + chunks.push(chunk); + }); + + res.on("end", (chunk) => { + let body = Buffer.concat(chunks); + console.log(body.toString()); + }); + + res.on("error", (error) => { + console.error(error); + }); + }); + + /*let postData = ` + ----------------------------650979185122712208150351 + Content-Disposition: form-data; name="file"; filename="one-does-not-simply.jpg" + Content-Type: image/jpeg + + ${fs.readFileSync(__dirname + "/one-does-not-simply.jpg").toString("ascii")} + ----------------------------650979185122712208150351 + Content-Disposition: form-data; name="initial_comment" + + Wisdom of the Masters + ----------------------------650979185122712208150351 + Content-Disposition: form-data; name="channels" + + CPPUV5KU0 + ----------------------------650979185122712208150351-- + `; + + fs.writeFileSync("temp-ascii.txt", postData); + + req.setHeader( + "content-type", + "multipart/form-data; boundary=--------------------------650979185122712208150351" + );*/ + + let postData = + '------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="file"; filename="one-does-not-simply.jpg"\r\nContent-Type: "image/jpeg"\r\n\r\n' + + fs.readFileSync(__dirname + "/one-does-not-simply.jpg") + + '\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="initial_comment"\r\n\r\nWisdom of the Masters\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="channels"\r\n\r\nCPPUV5KU0\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--'; + + req.setHeader( + "content-type", + "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW" + ); + + req.write(postData); + + req.end(); + }); +}; + (async () => { if (!process.env.BOT_USER_OAUTH_ACCESS_TOKEN || !process.env.CHANNEL) { console.error("Missing env values"); @@ -105,4 +181,9 @@ const testUpdateMessage = async (channel, token) => { process.env.CHANNEL, process.env.BOT_USER_OAUTH_ACCESS_TOKEN ); + + await testUploadFile( + process.env.CHANNEL, + process.env.BOT_USER_OAUTH_ACCESS_TOKEN + ); })(); diff --git a/integration-test/one-does-not-simply.jpg b/integration-test/one-does-not-simply.jpg new file mode 100644 index 0000000..300e1d2 Binary files /dev/null and b/integration-test/one-does-not-simply.jpg differ