Skip to content
This repository has been archived by the owner on Apr 26, 2021. It is now read-only.

Commit

Permalink
add GetDiff Handler
Browse files Browse the repository at this point in the history
  • Loading branch information
joaopaulovieira committed Jul 28, 2014
1 parent 3ec070d commit 838e0b1
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
23 changes: 23 additions & 0 deletions api/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,3 +378,26 @@ func GetTag(w http.ResponseWriter, r *http.Request) {
}
w.Write(b)
}

func GetDiff(w http.ResponseWriter, r *http.Request) {
repo := r.URL.Query().Get(":name")
previousCommit := r.URL.Query().Get("previous_commit")
lastCommit := r.URL.Query().Get("last_commit")
if repo == "" {
err := fmt.Errorf("Error when trying to obtain diff between hash commits of repository %s (repository is required).", repo)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
if previousCommit == "" || lastCommit == "" {
err := fmt.Errorf("Error when trying to obtain diff between hash commits of repository %s (Hash Commit(s) are required).", repo)
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
diff, err := repository.GetDiff(repo, previousCommit, lastCommit)
if err != nil {
http.Error(w, err.Error(), http.StatusNotFound)
return
}
w.Header().Set("Content-Length", strconv.Itoa(len(diff)))
w.Write(diff)
}
56 changes: 56 additions & 0 deletions api/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1095,3 +1095,59 @@ func (s *S) TestGetTag(c *gocheck.C) {
c.Assert(objLinks["zipArchive"], gocheck.Equals, links["zipArchive"])
c.Assert(objLinks["tarArchive"], gocheck.Equals, links["tarArchive"])
}

func (s *S) TestGetDiff(c *gocheck.C) {
url := "/repository/repo/diff/commits?:name=repo&previous_commit=1b970b076bbb30d708e262b402d4e31910e1dc10&last_commit=545b1904af34458704e2aa06ff1aaffad5289f8f"
expected := "test_diff"
repository.Retriever = &repository.MockContentRetriever{
ResultContents: []byte(expected),
}
defer func() {
repository.Retriever = nil
}()
request, err := http.NewRequest("GET", url, nil)
c.Assert(err, gocheck.IsNil)
recorder := httptest.NewRecorder()
GetDiff(recorder, request)
c.Assert(recorder.Code, gocheck.Equals, http.StatusOK)
c.Assert(recorder.Body.String(), gocheck.Equals, expected)
}

func (s *S) TestGetDiffWhenCommandFails(c *gocheck.C) {
url := "/repository/repo/diff/commits?:name=repo&previous_commit=1b970b076bbb30d708e262b402d4e31910e1dc10&last_commit=545b1904af34458704e2aa06ff1aaffad5289f8f"
outputError := fmt.Errorf("command error")
repository.Retriever = &repository.MockContentRetriever{
OutputError: outputError,
}
defer func() {
repository.Retriever = nil
}()
request, err := http.NewRequest("GET", url, nil)
c.Assert(err, gocheck.IsNil)
recorder := httptest.NewRecorder()
GetDiff(recorder, request)
c.Assert(recorder.Code, gocheck.Equals, http.StatusNotFound)
c.Assert(recorder.Body.String(), gocheck.Equals, "command error\n")
}

func (s *S) TestGetDiffWhenNoRepository(c *gocheck.C) {
url := "/repository//diff/commits?:name=&previous_commit=1b970b076bbb30d708e262b402d4e31910e1dc10&last_commit=545b1904af34458704e2aa06ff1aaffad5289f8f"
request, err := http.NewRequest("GET", url, nil)
c.Assert(err, gocheck.IsNil)
recorder := httptest.NewRecorder()
GetDiff(recorder, request)
expected := "Error when trying to obtain diff between hash commits of repository (repository is required).\n"
c.Assert(recorder.Code, gocheck.Equals, http.StatusBadRequest)
c.Assert(recorder.Body.String(), gocheck.Equals, expected)
}

func (s *S) TestGetDiffWhenNoCommits(c *gocheck.C) {
url := "/repository/repo/diff/commits?:name=repo&previous_commit=&last_commit="
request, err := http.NewRequest("GET", url, nil)
c.Assert(err, gocheck.IsNil)
recorder := httptest.NewRecorder()
GetDiff(recorder, request)
expected := "Error when trying to obtain diff between hash commits of repository repo (Hash Commit(s) are required).\n"
c.Assert(recorder.Code, gocheck.Equals, http.StatusBadRequest)
c.Assert(recorder.Body.String(), gocheck.Equals, expected)
}

0 comments on commit 838e0b1

Please sign in to comment.