|
| 1 | +package httpmock |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "os" |
| 8 | + "path" |
| 9 | + "strings" |
| 10 | +) |
| 11 | + |
| 12 | +// TODO: clean up methods in this file when there are no more callers |
| 13 | + |
| 14 | +func (r *Registry) StubResponse(status int, body io.Reader) { |
| 15 | + r.Register(MatchAny, func(*http.Request) (*http.Response, error) { |
| 16 | + return httpResponse(status, body), nil |
| 17 | + }) |
| 18 | +} |
| 19 | + |
| 20 | +func (r *Registry) StubWithFixture(status int, fixtureFileName string) func() { |
| 21 | + fixturePath := path.Join("../test/fixtures/", fixtureFileName) |
| 22 | + fixtureFile, err := os.Open(fixturePath) |
| 23 | + r.Register(MatchAny, func(*http.Request) (*http.Response, error) { |
| 24 | + if err != nil { |
| 25 | + return nil, err |
| 26 | + } |
| 27 | + return httpResponse(200, fixtureFile), nil |
| 28 | + }) |
| 29 | + return func() { |
| 30 | + if err == nil { |
| 31 | + fixtureFile.Close() |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +func (r *Registry) StubRepoResponse(owner, repo string) { |
| 37 | + r.StubRepoResponseWithPermission(owner, repo, "WRITE") |
| 38 | +} |
| 39 | + |
| 40 | +func (r *Registry) StubRepoResponseWithPermission(owner, repo, permission string) { |
| 41 | + r.Register(MatchAny, StringResponse(RepoNetworkStubResponse(owner, repo, "master", permission))) |
| 42 | +} |
| 43 | + |
| 44 | +func (r *Registry) StubRepoResponseWithDefaultBranch(owner, repo, defaultBranch string) { |
| 45 | + r.Register(MatchAny, StringResponse(RepoNetworkStubResponse(owner, repo, defaultBranch, "WRITE"))) |
| 46 | +} |
| 47 | + |
| 48 | +func (r *Registry) StubForkedRepoResponse(ownRepo, parentRepo string) { |
| 49 | + r.Register(MatchAny, StringResponse(RepoNetworkStubForkResponse(ownRepo, parentRepo))) |
| 50 | +} |
| 51 | + |
| 52 | +func RepoNetworkStubResponse(owner, repo, defaultBranch, permission string) string { |
| 53 | + return fmt.Sprintf(` |
| 54 | + { "data": { "repo_000": { |
| 55 | + "id": "REPOID", |
| 56 | + "name": "%s", |
| 57 | + "owner": {"login": "%s"}, |
| 58 | + "defaultBranchRef": { |
| 59 | + "name": "%s" |
| 60 | + }, |
| 61 | + "viewerPermission": "%s" |
| 62 | + } } } |
| 63 | + `, repo, owner, defaultBranch, permission) |
| 64 | +} |
| 65 | + |
| 66 | +func RepoNetworkStubForkResponse(forkFullName, parentFullName string) string { |
| 67 | + forkRepo := strings.SplitN(forkFullName, "/", 2) |
| 68 | + parentRepo := strings.SplitN(parentFullName, "/", 2) |
| 69 | + return fmt.Sprintf(` |
| 70 | + { "data": { "repo_000": { |
| 71 | + "id": "REPOID2", |
| 72 | + "name": "%s", |
| 73 | + "owner": {"login": "%s"}, |
| 74 | + "defaultBranchRef": { |
| 75 | + "name": "master" |
| 76 | + }, |
| 77 | + "viewerPermission": "ADMIN", |
| 78 | + "parent": { |
| 79 | + "id": "REPOID1", |
| 80 | + "name": "%s", |
| 81 | + "owner": {"login": "%s"}, |
| 82 | + "defaultBranchRef": { |
| 83 | + "name": "master" |
| 84 | + }, |
| 85 | + "viewerPermission": "READ" |
| 86 | + } |
| 87 | + } } } |
| 88 | + `, forkRepo[1], forkRepo[0], parentRepo[1], parentRepo[0]) |
| 89 | +} |
0 commit comments