Skip to content

Can mock APIs for HTTP but not HTTPS #52

@walkerab

Description

@walkerab

Describe the bug

We are able to mock HTTP API requests but when we attempt to mock HTTPS API requests, they instead pass through to the original, un-mocked endpoints.

To Reproduce

Here I've set up three mocks.

I added the moctokit one so I could be sure I wasn't doing the mock setup incorrectly.

Workflow
name: Mock API Test

on:
  pull_request:

jobs:
  metrics:
    name: Metrics
    runs-on: ubuntu-latest
    steps:
      - name: HTTP api call
        run: |
          result=$(curl -s http://google.com)
          echo "$result"

      - name: HTTPS api call
        run: |
          result=$(curl -s https://google.com)
          echo "$result"

      - run: |
          curl -s -L \
            -H "Accept: application/vnd.github+json" \
            -H "Authorization: Bearer ${{ github.token }}" \
            -H "X-GitHub-Api-Version: 2022-11-28" \
            -o response.json \
            https://api.github.com/rate_limit
          cat response.json
Jest Test Code
let { MockGithub, Moctokit } = require("@kie/mock-github")
let { Act, Mockapi } = require("@kie/act-js")
let path = require("path")

jest.setTimeout(60000)

let mockGithub
beforeEach(async () => {
  mockGithub = new MockGithub({
    repo: {
      testCompositeAction: {
        files: [
          {
            src: path.join(__dirname, "mock-api.test.yml"),
            dest: ".github/workflows/mock-api.test.yml",
          },
        ],
      },
    },
  })

  await mockGithub.setup()
})

afterEach(async () => {
  await mockGithub.teardown()
})

test("it mocks api calls", async () => {
  const moctokit = new Moctokit()
  const mockapi = new Mockapi({
    google_https: {
      baseUrl: "https://google.com",
      endpoints: {
        root: {
          get: {
            path: "/",
            method: "get",
            parameters: {
              query: [],
              path: [],
              body: [],
            },
          },
        },
      },
    },
    google_http: {
      baseUrl: "http://google.com",
      endpoints: {
        root: {
          get: {
            path: "/",
            method: "get",
            parameters: {
              query: [],
              path: [],
              body: [],
            },
          },
        },
      },
    }
  })
  const act = new Act(mockGithub.repo.getPath("testCompositeAction"))
    .setGithubToken("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") // fake token
  const result = await act.runEvent("pull_request", {
    logFile: path.join(__dirname, "../logs/metrics.log"),
    mockApi: [
      mockapi.mock.google_http.root
        .get()
        .setResponse({ status: 200, data: "mock response" }),
      mockapi.mock.google_https.root
        .get()
        .setResponse({ status: 200, data: "mock response" }),
      moctokit.rest.rateLimit
        .get()
        .setResponse({ status: 200, data: "mock response" }),
    ]
  })
  console.log(result)
})
Test Output
console.log
  [
    { name: 'Main HTTP api call', status: 0, output: 'mock response' },
    {
      name: 'Main HTTPS api call',
      status: 0,
      output: '<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">\n' +
        '<TITLE>301 Moved</TITLE></HEAD><BODY>\n' +
        '<H1>301 Moved</H1>\n' +
        'The document has moved\n' +
        '<A HREF="https://www.google.com/">here</A>.\n' +
        '</BODY></HTML>'
    },
    {
      name: 'Main curl -s -L \\',
      status: 0,
      output: '{\n' +
        '"message": "Bad credentials",\n' +
        '"documentation_url": "https://docs.github.com/rest"\n' +
        '}'
    }
  ]

Expected behavior

All three mocks should intercept the api calls and respond with 'mock response'.

Instead only the HTTP API request is being mocked. The other two are hitting the actual APIs.

Logs

Logs
[Mock API Test/Metrics] 🚀  Start image=ghcr.io/catthehacker/ubuntu:act-latest
[Mock API Test/Metrics]   🐳  docker pull image=ghcr.io/catthehacker/ubuntu:act-latest platform=linux/amd64 username= forcePull=true
[Mock API Test/Metrics]   🐳  docker create image=ghcr.io/catthehacker/ubuntu:act-latest platform=linux/amd64 entrypoint=["tail" "-f" "/dev/null"] cmd=[]
[Mock API Test/Metrics]   🐳  docker run image=ghcr.io/catthehacker/ubuntu:act-latest platform=linux/amd64 entrypoint=["tail" "-f" "/dev/null"] cmd=[]
[Mock API Test/Metrics] ⭐ Run Main HTTP api call
[Mock API Test/Metrics]   🐳  docker exec cmd=[bash --noprofile --norc -e -o pipefail /var/run/act/workflow/0] user= workdir=
[Mock API Test/Metrics]   | mock response
[Mock API Test/Metrics]   ✅  Success - Main HTTP api call
[Mock API Test/Metrics] ⭐ Run Main HTTPS api call
[Mock API Test/Metrics]   🐳  docker exec cmd=[bash --noprofile --norc -e -o pipefail /var/run/act/workflow/1] user= workdir=
[Mock API Test/Metrics]   | <HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
[Mock API Test/Metrics]   | <TITLE>301 Moved</TITLE></HEAD><BODY>
[Mock API Test/Metrics]   | <H1>301 Moved</H1>
[Mock API Test/Metrics]   | The document has moved
[Mock API Test/Metrics]   | <A HREF="https://www.google.com/">here</A>.
[Mock API Test/Metrics]   | </BODY></HTML>
[Mock API Test/Metrics]   ✅  Success - Main HTTPS api call
[Mock API Test/Metrics] ⭐ Run Main curl -s -L \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer ***" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  -o response.json \
  https://api.github.com/rate_limit
cat response.json
[Mock API Test/Metrics]   🐳  docker exec cmd=[bash --noprofile --norc -e -o pipefail /var/run/act/workflow/2] user= workdir=
[Mock API Test/Metrics]   | {
[Mock API Test/Metrics]   |   "message": "Bad credentials",
[Mock API Test/Metrics]   |   "documentation_url": "https://docs.github.com/rest"
[Mock API Test/Metrics]   | }
[Mock API Test/Metrics]   ✅  Success - Main curl -s -L \
  -H "Accept: application/vnd.github+json" \
  -H "Authorization: Bearer ***" \
  -H "X-GitHub-Api-Version: 2022-11-28" \
  -o response.json \
  https://api.github.com/rate_limit
cat response.json
[Mock API Test/Metrics] 🏁  Job succeeded

Additional context

I've tried this on a macbook (Ventura 13.5) as well as an AWS EC2 instance (Amazon Linux 2023) with the same results.

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions