🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more →
Socket
Book a DemoInstallSign in
Socket

github.com/ricardogama/api-mocker-go

Package Overview
Dependencies
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

github.com/ricardogama/api-mocker-go

Source
Go
Version
v1.0.2
Version published
Created
Source

api-mocker-go

Go SDK for api-mocker

Status

Build Status Coverage Status License: MIT

Installation

Install the package via go get or add to your dependency manager file:

$ go get github.com/ricardogama/api-mocker-go

Usage

Create a new mocker and use its sugar functions to communicate with the mocker server:

package main

import (
  "fmt"
  "net/http"

  "github.com/ricardogama/api-mocker-go"
)

func main() {
  mock := mocker.New("http://localhost:3000")
  defer mock.Clear() // DELETE /mocks

  // POST /mocks
  err := mock.Expect(&mocker.Request{
    Method: "POST",
    Path: "/foo",
    Body: map[string]interface{}{
      "foo": "bar",
    },
    Headers: map[string]string{
      "X-User-ID": "1",
    },
    Response: &mocker.Response{
      Status: 200,
      Headers: map[string]string{
        "Content-Range": "bytes 200-1000/67589",
      },
      Body: map[string]interface{}{
        "qux": "qix",
      },
    },
  })

  if err != nil {
    panic(err)
  }

  _, err := http.Get("http://localhost:3000/foo")
  if err != nil {
      panic(err)
  }

  // GET /mocks, returning an error if there's unexpected requests.
  err := mock.Ensure()
  fmt.Println(err)
  // missing 1 expected calls: [
  //     {
  //         "headers": {
  //             "X-User-ID": "1"
  //         },
  //         "body": {
  //             "foo": "bar"
  //         },
  //         "method": "POST",
  //         "path": "/foo",
  //         "response": {
  //             "headers": {
  //                 "Content-Range": "bytes 200-1000/67589"
  //             },
  //             "body": {
  //                 "qux": "qix"
  //             },
  //             "status": 200
  //         }
  //     }
  // ]
  // 1 unexpected calls: [
  //     {
  //         "headers": {
  //             "accept-encoding": "gzip",
  //             "host": "localhost:3005",
  //             "user-agent": "Go-http-client/1.1"
  //         },
  //         "body": {},
  //         "method": "GET",
  //         "path": "/foo",
  //         "response": null
  //     }
  // ]
  })
}

License

MIT

FAQs

Package last updated on 25 Feb 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts