githubtest

package
v0.7.18 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2026 License: Apache-2.0 Imports: 20 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package githubtest is a fake GitHub for tests of code that reads and writes repos: the mounts GitHub driver, the storage example app, and consumers' own tests. It serves the REST routes the driver uses (a repo, a commit, a branch's commits, a tarball, and the Contents API) and the GraphQL tags query over repos held in memory, and each write makes a real commit on its branch. It serves the hooks API too, and Push delivers a signed push to each hook made through it. PushRequest and SignedRequest build signed webhook deliveries for an app's handler.

A private repo answers only a request with an accepted token, as GitHub does, with 404 for anything else. Once a private repo has grants (Repo.Grant), only a granted token reads it. Every request is recorded with the token it carried, so a test can check that a connection's token reached each one.

oneauth's outbound/githubtest does a different job, minting and refreshing tokens. A test that drives a connection end to end uses both, with AcceptToken trusting the tokens the other mints.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func SignedRequest added in v0.7.16

func SignedRequest(target string, secret []byte, event string, body []byte) *http.Request

SignedRequest builds a webhook delivery to target as GitHub sends one: a POST of body with the event name in X-GitHub-Event and X-Hub-Signature-256 set to the HMAC-SHA256 of body under secret. Serve it to an app's handler with httptest.NewRecorder, or send it with http.DefaultClient.

Types

type Hook added in v0.7.18

type Hook struct {
	ID          int64
	URL         string
	ContentType string
	Secret      string
	Events      []string
	Active      bool
	Token       string
}

Hook is a webhook made on a repo through the hooks API. Token is the token that last made or changed it, so a test can check that the app used the right connection.

type Repo

type Repo struct {
	Owner   string
	Name    string
	Private bool
	// contains filtered or unexported fields
}

Repo is one repository in the fake. Its methods are safe to call while the server runs.

func (*Repo) AddTarEntries

func (r *Repo) AddTarEntries(sha string, es ...TarEntry)

AddTarEntries adds entries to the tarball of commit sha only. The Contents API doesn't see them.

func (*Repo) Commit

func (r *Repo) Commit(branch string, put map[string]string, del []string) string

Commit makes a commit on branch, creating the branch if needed. It starts from the branch's current files, sets each of put, and removes each of del. It returns the new commit's sha.

func (*Repo) File

func (r *Repo) File(branch, p string) (string, bool)

File returns the contents of p at the head of branch.

func (*Repo) Grant added in v0.7.17

func (r *Repo) Grant(tokens ...string)

Grant lets each of tokens read the repo, and accepts each one as AddToken does. Once a private repo has a grant, it answers only granted tokens, and gives any other token 404, accepted or not, as GitHub does for a repo the token can't see. A private repo with no grants answers any accepted token. A public repo answers everyone either way.

func (*Repo) Head

func (r *Repo) Head(branch string) string

Head returns the commit sha branch points at, or "" for a missing branch.

func (*Repo) HookManagers added in v0.7.18

func (r *Repo) HookManagers(tokens ...string)

HookManagers limits who may manage the repo's hooks to tokens, and accepts each one as AddToken does. Any other token gets 404 from the hooks API, as GitHub answers a token without admin rights on the repo. With no managers, any accepted token that can see the repo manages its hooks.

func (*Repo) Hooks added in v0.7.18

func (r *Repo) Hooks() []Hook

Hooks returns a copy of the repo's hooks, oldest first.

func (*Repo) Push added in v0.7.18

func (r *Repo) Push(branch string, put map[string]string, del []string) (string, error)

Push commits put and del on branch, as Commit does, then delivers a signed push event to each active hook that wants push events, one at a time, before it returns. GitHub delivers later and in the background; delivering first lets a test check the app's state right after Push. Each delivery is JSON, whatever the hook's content type. Push returns the new commit, and the first delivery that fails or answers other than 2xx.

func (*Repo) PushRequest added in v0.7.16

func (r *Repo) PushRequest(target string, secret []byte, branch string) *http.Request

PushRequest builds a signed push event to target for branch's head, with its parent as before, as GitHub sends after a push of one commit. It panics on a missing branch.

func (*Repo) SetCommitTime added in v0.7.16

func (r *Repo) SetCommitTime(sha string, t time.Time)

SetCommitTime sets commit sha's date, so a test can make the order of tags by date differ from their order by name. It panics on a sha the repo doesn't have.

func (*Repo) Source

func (r *Repo) Source() string

Source returns the repo's mounts source, "github://owner/name".

func (r *Repo) Symlink(branch, p, target string) string

Symlink makes a commit on branch adding a link at p to target.

func (*Repo) Tag added in v0.7.16

func (r *Repo) Tag(name, sha string, annotated bool)

Tag points the tag name at commit sha, replacing any tag of that name. An annotated tag is served as GitHub serves one, through a tag object. It panics on a sha the repo doesn't have.

type Request

type Request struct {
	Method string
	Path   string

	// Token is the bearer or "token" credential the request carried, or "" for none.
	Token string
}

Request is one request the fake answered.

type Server

type Server struct {
	*httptest.Server
	// contains filtered or unexported fields
}

Server is the fake. Its URL is the API base a driver is pointed at.

func New

func New(t testing.TB) *Server

New starts a fake and stops it when the test ends.

func (*Server) AcceptToken

func (s *Server) AcceptToken(f func(token string) bool)

AcceptToken also accepts any token f approves, such as one oneauth's fake minted (its ValidInstallationToken or ValidUserToken).

func (*Server) AddRepo

func (s *Server) AddRepo(owner, name string, private bool, files map[string]string) *Repo

AddRepo creates owner/name with one commit on branch "main" holding files, and returns it.

func (*Server) AddToken

func (s *Server) AddToken(tok string)

AddToken makes tok an accepted token.

func (*Server) CorruptRaw added in v0.7.14

func (s *Server) CorruptRaw(on bool)

CorruptRaw makes the raw host and the git blobs endpoint send each file with one byte added, so a test can check that a driver refuses bytes that don't match their git blob id.

func (*Server) FailNext

func (s *Server) FailNext(status int)

FailNext makes the next request answer with status and no body, then serve normally. 403 sets x-ratelimit-remaining to 0, as GitHub's rate limit does.

func (*Server) RawURL added in v0.7.14

func (s *Server) RawURL() string

RawURL is the fake's raw host, which serves a file by owner, repo, commit and path as raw.githubusercontent.com does. Point a driver at it with WithRawURL.

func (*Server) Requests

func (s *Server) Requests() []Request

Requests returns every request answered so far, oldest first.

type TarEntry

type TarEntry struct {
	Name     string // relative to the tarball's top folder
	Type     byte   // tar.TypeReg, tar.TypeSymlink, ...
	Body     string
	Linkname string
}

TarEntry is an extra entry a test adds to a commit's tarball, such as a link or a path that climbs out, to check that a driver refuses it.

Jump to

Keyboard shortcuts

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