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. 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. 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 ¶
- func SignedRequest(target string, secret []byte, event string, body []byte) *http.Request
- type Repo
- func (r *Repo) AddTarEntries(sha string, es ...TarEntry)
- func (r *Repo) Commit(branch string, put map[string]string, del []string) string
- func (r *Repo) File(branch, p string) (string, bool)
- func (r *Repo) Head(branch string) string
- func (r *Repo) PushRequest(target string, secret []byte, branch string) *http.Request
- func (r *Repo) SetCommitTime(sha string, t time.Time)
- func (r *Repo) Source() string
- func (r *Repo) Symlink(branch, p, target string) string
- func (r *Repo) Tag(name, sha string, annotated bool)
- type Request
- type Server
- func (s *Server) AcceptToken(f func(token string) bool)
- func (s *Server) AddRepo(owner, name string, private bool, files map[string]string) *Repo
- func (s *Server) AddToken(tok string)
- func (s *Server) CorruptRaw(on bool)
- func (s *Server) FailNext(status int)
- func (s *Server) RawURL() string
- func (s *Server) Requests() []Request
- type TarEntry
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SignedRequest ¶ added in v0.7.16
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 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 ¶
AddTarEntries adds entries to the tarball of commit sha only. The Contents API doesn't see them.
func (*Repo) Commit ¶
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) PushRequest ¶ added in v0.7.16
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
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.
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 ¶
Server is the fake. Its URL is the API base a driver is pointed at.
func (*Server) AcceptToken ¶
AcceptToken also accepts any token f approves, such as one oneauth's fake minted (its ValidInstallationToken or ValidUserToken).
func (*Server) AddRepo ¶
AddRepo creates owner/name with one commit on branch "main" holding files, and returns it.
func (*Server) CorruptRaw ¶ added in v0.7.14
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 ¶
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.
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.