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

The API-KEY is sent as header entry. #97

Open
wants to merge 4 commits into
base: master
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "route4me-nodejs-sdk",
"version": "1.0.28",
"version": "1.0.29",
"description": "Access Route4Me's logistics-as-a-service API using our Node.js SDK",
"main": "src/index.js",
"browser": "src/route4me.js",
Expand Down
15 changes: 14 additions & 1 deletion src/request-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,19 @@ class RequestManager {
apiUrl = (options["v5"] ? `${this._baseUrl5}${options.path}` : `${this._baseUrl}${options.path}`)
}

qs["api_key"] = this._apiKey
let urlObject = null
try {
urlObject = new URL(apiUrl)
} catch (err) {
//
}

const authorization = { type: "" }
if (urlObject && "wh" === urlObject.hostname.substring(0, 2).toLocaleLowerCase()) {
authorization.type = "bearer"
} else {
qs["api_key"] = this._apiKey
}

if (undefined === options.validationContext) {
// this is just a protective wall
Expand Down Expand Up @@ -244,6 +256,7 @@ class RequestManager {

const req = request[method](apiUrl)
.set("Route4Me-User-Agent", this._userAgent)
.auth(this._apiKey, "", authorization)
.timeout(timeouts)
.redirects(1000) // unlimited number of redirects
.accept("application/json")
Expand Down
30 changes: 18 additions & 12 deletions test/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,36 @@ const _ = require("lodash")
const runIntegrationTests = "1" === process.env["TEST_INTEGRATION"]
//const describeIntegration = runIntegrationTests ? describe : describe.skip
const describeIntegration = describe
function expectRequest(req, method, url, query, body, contentType /* , form */) {
function expectRequest(req, method, url, query, body, contentType) {
const ct = contentType || "application/json"

expect(req).has.property("url")
.and.is.equal(url)

const urlObject = new URL(req.url)
const api_v4 = ("wh" !== urlObject.hostname.substring(0, 2).toLocaleLowerCase())

expect(req).has.property("method")
.and.is.equal(method)

expect(req).has.property("headers")
.that.has.property("content-type", ct)

// With API v5 the API-KEY is sent as header entry rather than as URL parameter.
if (!api_v4) {
expect(req.headers).has.property("authorization")
}

// QUERY assertions
expect(req).has.property("query")
.with.property("api_key")
.that.is.exist
.that.not.oneOf(["null", "undefined", ""])
if (api_v4) {
expect(req).has.property("query")
.with.property("api_key")
.that.is.exist
.that.not.oneOf(["null", "undefined", ""])
} else {
expect(req).has.property("query")
.that.not.oneOf(["null", "undefined", ""])
}

const qs = _({})
.merge(req.query)
Expand All @@ -43,13 +56,6 @@ function expectRequest(req, method, url, query, body, contentType /* , form */)
expect(req).has.property("body")
.and.is.null
}

// if (form) {
// expect(req).has.property("form")
// .that.is.deep.equal(form)
// } else {
// expect(req).has.not.property("form")
// }
}

function toSuiteName(filename) {
Expand Down