2020-01-15 19:41:47 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2020 Balena Ltd.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import Bluebird = require('bluebird');
|
2020-01-24 01:21:51 +00:00
|
|
|
import * as path from 'path';
|
2020-01-15 19:41:47 +00:00
|
|
|
import * as zlib from 'zlib';
|
|
|
|
|
|
|
|
import { NockMock } from './nock-mock';
|
|
|
|
|
2020-01-24 01:21:51 +00:00
|
|
|
export const builderResponsePath = path.normalize(
|
|
|
|
path.join(__dirname, 'test-data', 'builder-response'),
|
|
|
|
);
|
|
|
|
|
2020-01-15 19:41:47 +00:00
|
|
|
export class BuilderMock extends NockMock {
|
|
|
|
constructor() {
|
|
|
|
super('https://builder.balena-cloud.com');
|
|
|
|
}
|
|
|
|
|
|
|
|
public expectPostBuild(opts: {
|
|
|
|
optional?: boolean;
|
|
|
|
persist?: boolean;
|
|
|
|
responseBody: any;
|
|
|
|
responseCode: number;
|
2020-01-24 01:21:51 +00:00
|
|
|
checkURI: (uri: string) => Promise<void>;
|
2020-01-15 19:41:47 +00:00
|
|
|
checkBuildRequestBody: (requestBody: string | Buffer) => Promise<void>;
|
|
|
|
}) {
|
|
|
|
this.optPost(/^\/v3\/build($|[(?])/, opts).reply(async function(
|
2020-01-24 01:21:51 +00:00
|
|
|
uri,
|
2020-01-15 19:41:47 +00:00
|
|
|
requestBody,
|
|
|
|
callback,
|
|
|
|
) {
|
|
|
|
let error: Error | null = null;
|
|
|
|
try {
|
2020-01-24 01:21:51 +00:00
|
|
|
await opts.checkURI(uri);
|
2020-01-15 19:41:47 +00:00
|
|
|
if (typeof requestBody === 'string') {
|
|
|
|
const gzipped = Buffer.from(requestBody, 'hex');
|
|
|
|
const gunzipped = await Bluebird.fromCallback<Buffer>(cb => {
|
|
|
|
zlib.gunzip(gzipped, cb);
|
|
|
|
});
|
|
|
|
await opts.checkBuildRequestBody(gunzipped);
|
|
|
|
} else {
|
|
|
|
throw new Error(
|
|
|
|
`unexpected requestBody type "${typeof requestBody}"`,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
error = err;
|
|
|
|
}
|
|
|
|
callback(error, [opts.responseCode, opts.responseBody]);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|