Skip to content

Commit

Permalink
refactor: split route table to http method based functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cybersiddhu committed Dec 9, 2020
1 parent 3aecdbb commit dfa1d2f
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions internal/fake/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,35 @@ type route struct {
fn httpFunc
}

func routeTable() []*route {
func fetchRoute() []*route {
return []*route{
{
method: "DELETE",
file: "",
fn: handleNoContent,
method: "GET",
file: "../../../testdata/get-repo.json",
fn: handleSuccess,
regexp: regexp.MustCompile(
fmt.Sprintf(
"^%s%s$",
baseURLPath,
`/repos/([^/]+)/([^/]+)`,
)),
},
{
file: "../../testdata/commit-diff.json",
fn: handleSuccess,
method: "GET",
regexp: regexp.MustCompile(
fmt.Sprintf(
"^%s%s$",
baseURLPath,
`/repos/([^/]+)/([^/]+)/compare/\w+\.\.\.\w+`,
)),
},
}
}

func postRoute() []*route {
return []*route{
{
method: "PATCH",
file: "../../../testdata/edit-repo.json",
Expand All @@ -60,31 +76,32 @@ func routeTable() []*route {
`/repos/([^/]+)/([^/]+)/forks`,
)),
},
}
}

func delRoute() []*route {
return []*route{
{
method: "GET",
file: "../../../testdata/get-repo.json",
fn: handleSuccess,
method: "DELETE",
file: "",
fn: handleNoContent,
regexp: regexp.MustCompile(
fmt.Sprintf(
"^%s%s$",
baseURLPath,
`/repos/([^/]+)/([^/]+)`,
)),
},
{
file: "../../testdata/commit-diff.json",
fn: handleSuccess,
method: "GET",
regexp: regexp.MustCompile(
fmt.Sprintf(
"^%s%s$",
baseURLPath,
`/repos/([^/]+)/([^/]+)/compare/\w+\.\.\.\w+`,
)),
},
}
}

func routeTable() []*route {
var route []*route
route = append(route, fetchRoute()...)
route = append(route, postRoute()...)
return append(route, delRoute()...)
}

func handleNoContent(file string, w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusNoContent)
if _, err := fmt.Fprint(w, file); err != nil {
Expand Down

0 comments on commit dfa1d2f

Please sign in to comment.