Skip to content
This repository has been archived by the owner on Jul 2, 2020. It is now read-only.

invoke 参数 data 传递不符合预期 #107

Closed
bear-ei opened this issue Apr 3, 2020 · 1 comment · Fixed by #111
Closed

invoke 参数 data 传递不符合预期 #107

bear-ei opened this issue Apr 3, 2020 · 1 comment · Fixed by #111
Assignees

Comments

@bear-ei
Copy link

bear-ei commented Apr 3, 2020

./f.yml

service: serverless-hello-world

provider:
  name: aliyun

functions:
  index:
    handler: index.handler
    events:
      - http:
          method: post

package:
  artifact: code.zip

./test/index.test.ts

import { invoke } from '@midwayjs/serverless-invoke'
import * as assert from 'assert'
import { join } from 'path'

describe('/test/index.test.ts', () => {
  it('invoke', async () => {
    const result: any = await invoke({
      functionName: 'index',
      functionDir: join(__dirname, '../'),
      data: [
        {
          headers: { 'Content-Type': 'text/json' },
          method: 'POST',
          path: '/test',
          body: {
            name: 'test'
          }
        }
      ]
    })
    assert(/hello world/.test(result))
  })
})

./src/index.ts

import {
  FaaSContext,
  func,
  inject,
  provide,
  FunctionHandler
} from '@midwayjs/faas'

@provide()
@func('index.handler')
export class IndexService implements FunctionHandler {
  @inject()
  ctx: FaaSContext // context

  async handler() {
    /**
     * expect { 'Content-Type': 'text/json' }
     * actual { host: '127.0.0.1:55131','user-agent': 'curl/7.64.1'}
     */
    console.info(this.ctx.request.headers)

    /**
     * expect POST
     * actual GET
     */
    console.info(this.ctx.request.method)

    /**
     * expect /test
     * actual /
     */
    console.info(this.ctx.request.path)

    /**
     * expect { name: 'test' }
     * actual undefined
     */
    console.info(this.ctx.request.body)

    return 'hello world'
  }
}
@czy88840616
Copy link
Member

got

echosoar added a commit that referenced this issue Apr 5, 2020
czy88840616 added a commit that referenced this issue Apr 5, 2020
* fix: core support stop

* fix: #107

Co-authored-by: Harry Chen <czy88840616@gmail.com>
@bear-ei bear-ei mentioned this issue Apr 7, 2020
2 tasks
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants