Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Logging #1063

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions spec/integration.spec.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import twilio from "../src/";
import nock from "nock";
import { jest } from "@jest/globals";

var accountSid = "ACaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
var token = "token";
Expand Down Expand Up @@ -46,4 +48,52 @@ describe("twilio", function () {
expect(client.httpClient.autoRetry).toBe(true);
expect(client.httpClient.maxRetries).toBe(5);
});

describe('logging', function () {
let consoleSpy;

beforeEach(() => {
consoleSpy = jest.spyOn(console, 'log').mockImplementation();
});

afterEach(() => {
consoleSpy.mockRestore();
});

it('should use the log-level during http requests', async function () {
const scope = nock("https://api.twilio.com")
.get(`/2010-04-01/Accounts/${accountSid}/Messages.json?PageSize=1`)
.reply(200, {
"first_page_uri": `/2010-04-01/Accounts/${accountSid}/Usage/Records/Daily.json?Category=sms-outbound&PageSize=1&Page=0`,
"end": 0,
"previous_page_uri": null,
"uri": "/2010-04-01/Accounts/${accountSid}/Usage/Records/Daily.json?Category=sms-outbound&PageSize=1&Page=0",
"page_size": 1,
"start": 0,
"usage_records": [],
"next_page_uri": null,
"page": 0
});

const client = new twilio.Twilio(accountSid, token, {
logLevel: "debug",
});

await client.messages.list({limit: 1});
expect(consoleSpy.mock.calls.map((a) => a[0]).join("\n")).toBe(
"-- BEGIN Twilio API Request --\n" +
`get https://api.twilio.com/2010-04-01/Accounts/${accountSid}/Messages.json\n` +
"Querystring:\n" +
"[object Object]\n" +
"Headers:\n" +
"Accept: undefined\n" +
"User-Agent: undefined\n" +
"Accept-Charset: undefined\n" +
"Connection: undefined\n" +
"-- END Twilio API Request --\n" +
"response.statusCode: 200\n" +
"response.headers: {\"content-type\":\"application/json\"}");
scope.done();
});
});
});
3 changes: 2 additions & 1 deletion src/base/BaseTwilio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ namespace Twilio {
data: opts.data,
timeout: opts.timeout,
allowRedirects: opts.allowRedirects,
logLevel: opts.logLevel,
// use the Twilio client's log-level if the httpClient's log-level is unspecified
logLevel: opts.logLevel || this.opts?.logLevel,
});
}

Expand Down
Loading