ditto

package module
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 12, 2024 License: MIT Imports: 8 Imported by: 0

README

ditto

Ditto is a dead simple code-gen free, mock free, CLI tool free, API call caching package for testing 3rd party APIs in Go.

You shouldn't have to mock 3rd party APIs to test your code. Ditto is a simple package that caches API responses for you to use in your tests.

All you have to do is replace your API client's http.Client with ditto.Client when writing your tests and you're good to go. Ditto checks if the request has been made before and if so, returns the cached response. If not, it makes the request and caches the response for you to run your tests against later on.

Install

go get github.com/TimothyStiles/ditto

Usage

https://github.com/TimothyStiles/ditto/blob/da8d0f083d92b16aa99029b9dfbc8edb862bbf5b/example_test.go#L11-L24

Documentation

Overview

Example (Basic)
package main

import (
	"context"
	"fmt"
	"os"

	"github.com/TimothyStiles/ditto"
	"github.com/google/go-github/v57/github"
)

func main() {
	token := os.Getenv("GITHUB_TOKEN")

	// instead of http.DefaultClient we use ditto.Client()
	client := github.NewClient(ditto.Client()).WithAuthToken(token) // auth token is optional

	// Use client...
	repos, _, err := client.Repositories.List(context.Background(), "octocat", nil)
	if err != nil {
		fmt.Println("Error:", err)
		return
	}

	fmt.Println(repos[0].GetName())
}
Output:

boysenberry-repo-1

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Client

func Client() *http.Client

Types

type CachedResponse

type CachedResponse struct {
	StatusCode int
	Status     string
	Method     string
	URL        string
	Header     http.Header
	Body       string
}

type CachingTransport

type CachingTransport struct {
	Transport http.RoundTripper
}

func (*CachingTransport) RoundTrip

func (c *CachingTransport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip implements the RoundTripper interface.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL