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

Commit

Permalink
gandalftest: implement getRepository in testing server
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco Souza committed Feb 10, 2015
1 parent da32b62 commit 275ef89
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
45 changes: 33 additions & 12 deletions gandalftest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type GandalfServer struct {
keys map[string][]key
repos []repository.Repository
usersLock sync.RWMutex
repoLock sync.Mutex
repoLock sync.RWMutex
failures chan Failure
}

Expand Down Expand Up @@ -103,6 +103,7 @@ func (s *GandalfServer) buildMuxer() {
s.muxer.Delete("/user/{name}", http.HandlerFunc(s.removeUser))
s.muxer.Post("/repository", http.HandlerFunc(s.createRepository))
s.muxer.Delete("/repository/{name}", http.HandlerFunc(s.removeRepository))
s.muxer.Get("/repository/{name}", http.HandlerFunc(s.getRepository))
}

func (s *GandalfServer) createUser(w http.ResponseWriter, r *http.Request) {
Expand All @@ -129,13 +130,13 @@ func (s *GandalfServer) createUser(w http.ResponseWriter, r *http.Request) {

func (s *GandalfServer) removeUser(w http.ResponseWriter, r *http.Request) {
username := r.URL.Query().Get(":name")
s.usersLock.Lock()
defer s.usersLock.Unlock()
_, index := s.findUser(username)
if index < 0 {
http.Error(w, "user not found", http.StatusNotFound)
return
}
s.usersLock.Lock()
defer s.usersLock.Unlock()
last := len(s.users) - 1
s.users[index] = s.users[last]
s.users = s.users[:last]
Expand Down Expand Up @@ -171,25 +172,34 @@ func (s *GandalfServer) createRepository(w http.ResponseWriter, r *http.Request)

func (s *GandalfServer) removeRepository(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get(":name")
s.repoLock.Lock()
defer s.repoLock.Unlock()
index := -1
for i, repo := range s.repos {
if repo.Name == name {
index = i
break
}
}
_, index := s.findRepository(name)
if index < 0 {
http.Error(w, "repository not found", http.StatusNotFound)
return
}
s.repoLock.Lock()
defer s.repoLock.Unlock()
last := len(s.repos) - 1
s.repos[index] = s.repos[last]
s.repos = s.repos[:last]
}

func (s *GandalfServer) getRepository(w http.ResponseWriter, r *http.Request) {
name := r.URL.Query().Get(":name")
repo, index := s.findRepository(name)
if index < 0 {
http.Error(w, "repository not found", http.StatusNotFound)
return
}
err := json.NewEncoder(w).Encode(repo)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}

func (s *GandalfServer) findUser(name string) (username string, index int) {
s.usersLock.RLock()
defer s.usersLock.RUnlock()
for i, user := range s.users {
if user == name {
return user, i
Expand All @@ -198,6 +208,17 @@ func (s *GandalfServer) findUser(name string) (username string, index int) {
return "", -1
}

func (s *GandalfServer) findRepository(name string) (repository.Repository, int) {
s.repoLock.RLock()
defer s.repoLock.RUnlock()
for i, repo := range s.repos {
if repo.Name == name {
return repo, i
}
}
return repository.Repository{}, -1
}

func (s *GandalfServer) getFailure(method, path string) (Failure, bool) {
var f Failure
select {
Expand Down
28 changes: 28 additions & 0 deletions gandalftest/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package gandalftest

import (
"encoding/json"
"net"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -188,6 +189,33 @@ func (s *S) TestRemoveRepositoryNotFound(c *check.C) {
c.Assert(recorder.Body.String(), check.Equals, "repository not found\n")
}

func (s *S) TestGetRepository(c *check.C) {
repo := repository.Repository{Name: "somerepo"}
server, err := NewServer("127.0.0.1:0")
c.Assert(err, check.IsNil)
defer server.Stop()
server.repos = []repository.Repository{repo}
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/repository/somerepo", nil)
server.ServeHTTP(recorder, request)
c.Assert(recorder.Code, check.Equals, http.StatusOK)
var got repository.Repository
err = json.NewDecoder(recorder.Body).Decode(&got)
c.Assert(err, check.IsNil)
c.Assert(got, check.DeepEquals, repo)
}

func (s *S) TestGetRepositoryNotFound(c *check.C) {
server, err := NewServer("127.0.0.1:0")
c.Assert(err, check.IsNil)
defer server.Stop()
recorder := httptest.NewRecorder()
request, _ := http.NewRequest("GET", "/repository/somerepo", nil)
server.ServeHTTP(recorder, request)
c.Assert(recorder.Code, check.Equals, http.StatusNotFound)
c.Assert(recorder.Body.String(), check.Equals, "repository not found\n")
}

func (s *S) TestPrepareFailure(c *check.C) {
server, err := NewServer("127.0.0.1:0")
c.Assert(err, check.IsNil)
Expand Down

0 comments on commit 275ef89

Please sign in to comment.