Skip to content

Commit

Permalink
fixes #7
Browse files Browse the repository at this point in the history
  • Loading branch information
activescott committed Jan 8, 2018
1 parent cb42f89 commit 05cf1b0
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
26 changes: 26 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"label": "test (yarn)",
"type": "shell",
"command": "yarn test",
"options": {
"cwd": "${workspaceFolder}"
},
"presentation": {
"echo": true,
"reveal": "always",
"focus": false,
"panel": "dedicated"
},
"problemMatcher": [],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class ServerlessInvoker {
findServicePath () {
let dir = process.cwd()
while (dir !== '/' && !fs.existsSync(path.join(dir, 'serverless.yml'))) {
console.log('dir:', dir)
dir = path.dirname(dir)
}
if (dir === '/') {
Expand All @@ -37,6 +36,7 @@ class ServerlessInvoker {
}
if (!this.serverless) {
const sls = new Serverless(config)
// NOTE: I've seen sls.init() run very slowly; nearly 500ms!
return sls.init().then(() => {
return sls.variables.populateService().then(() => {
sls.service.setFunctionNames({})
Expand Down Expand Up @@ -67,7 +67,10 @@ class ServerlessInvoker {
throw new Error(`Serverless http event not found for HTTP request "${httpRequest}".`)
}

const parsedPath = ServerlessInvoker.parsePath(httpRequest)
event = Object.assign({}, event, {
path: parsedPath,
resource: parsedPath,
pathParameters: ServerlessInvoker.parsePathParameters(httpEvent, httpRequest),
queryStringParameters: ServerlessInvoker.parseQueryStringParameters(httpRequest),
httpMethod: ServerlessInvoker.parseHttpMethod(httpRequest)
Expand Down Expand Up @@ -127,6 +130,11 @@ class ServerlessInvoker {
return querystring.parse(search)
}

static parsePath (requestUrl) {
const myURL = new URL('https://fakehost.com/' + requestUrl.split(' ')[1])
return myURL.pathname
}

loadServerlessEnvironment () {
return Promise.try(() => {
let env = this.serverless.service.provider.environment
Expand Down
10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,16 @@ describe('serverless-http-invoker', function () {
return expect(response).to.eventually.have.deep.nested.property('body.input.httpMethod', 'GET')
})

it('should have event.path', function () {
let response = sls.invoke('GET api/hello')
return expect(response).to.eventually.have.deep.nested.property('body.input.path', '/api/hello')
})

it('should have event.resource', function () {
let response = sls.invoke('GET api/hello')
return expect(response).to.eventually.have.deep.nested.property('body.input.resource', '/api/hello')
})

it('should invoke path with params', function () {
let response = sls.invoke('GET api/hello/world')
return expect(response).to.eventually.have.property('statusCode', 200)
Expand Down

0 comments on commit 05cf1b0

Please sign in to comment.