Skip to content

Commit

Permalink
Use round-trip test for gzip instead of absolute value
Browse files Browse the repository at this point in the history
  • Loading branch information
jen20 committed Mar 13, 2019
1 parent 7cdd3ca commit 51be5ce
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
14 changes: 7 additions & 7 deletions sdk/nodejs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ import * as crypto from "crypto";
import * as fs from "fs";
import * as os from "os";
import * as Path from "path";
import { sprintf } from "sprintf-js";
import { v4 } from "uuid";
import { gzipSync } from "zlib";
import {sprintf} from "sprintf-js";
import {v4} from "uuid";
import {gzipSync} from "zlib";
import * as zlib from "zlib";

const titleCase = require("title-case");

Expand Down Expand Up @@ -77,10 +78,9 @@ export function base64encode(unencoded: string): string {
* @param unencoded the string to compress and encode
*/
export function base64gzip(unencoded: string): string {
// TODO(jen20): The zlib default options differ from those in Go, such that the output is different
// from Terraform (as per the tests, though this may also differ by which version of Go Terraform has
// been compiled with. To make the tests pass, figure out which options are different.
const compressed = gzipSync(unencoded);
const compressed = gzipSync(unencoded, {
level: zlib.constants.Z_DEFAULT_COMPRESSION,
});
return compressed.toString("base64");
}

Expand Down
7 changes: 5 additions & 2 deletions sdk/nodejs/tests/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
// limitations under the License.

import * as bcryptjs from "bcryptjs";
import { assert } from "chai";
import {assert} from "chai";
import * as os from "os";
import * as sut from "../index";
import {gunzipSync} from "zlib";

const validator = require("validator");

Expand Down Expand Up @@ -56,7 +57,9 @@ describe("base64encode", () => {

describe("base64gzip", () => {
it("works", () => {
assert.equal(sut.base64gzip("test"), "H4sIAAAAAAAA/ypJLS4BAAAA//8BAAD//wx+f9gEAAAA");
const zipped = sut.base64gzip("test");

assert.equal(gunzipSync(Buffer.from(zipped, 'base64')).toString('utf-8'), 'test');
});
});

Expand Down

0 comments on commit 51be5ce

Please sign in to comment.