Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/api/clients/restclient/restclient.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package restclient

import (
"net/http"
"encoding/json"
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"strings"
)

var (
Expand All @@ -17,9 +19,15 @@ type Mock struct {
Url string
HttpMethod string
Response *http.Response
BodyText string
Err error
}

func (m *Mock) GetResponse() *http.Response {
m.Response.Body = ioutil.NopCloser(strings.NewReader(m.BodyText))
return m.Response
}

func getMockId(httpMethod string, url string) string {
return fmt.Sprintf("%s_%s", httpMethod, url)
}
Expand All @@ -46,7 +54,8 @@ func Post(url string, body interface{}, headers http.Header) (*http.Response, er
if mock == nil {
return nil, errors.New("no mockup found for give request")
}
return mock.Response, mock.Err
// here lies the magic
return mock.GetResponse(), mock.Err
}

jsonBytes, err := json.Marshal(body)
Expand Down
41 changes: 18 additions & 23 deletions src/api/services/repositories_service_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package services

import (
"testing"
"github.com/federicoleon/golang-microservices/src/api/clients/restclient"
"os"
"github.com/federicoleon/golang-microservices/src/api/domain/repositories"
"github.com/federicoleon/golang-microservices/src/api/utils/errors"
"github.com/stretchr/testify/assert"
"net/http"
"io/ioutil"
"strings"
"os"
"sync"
"github.com/federicoleon/golang-microservices/src/api/utils/errors"
"testing"
)

func TestMain(m *testing.M) {
Expand All @@ -34,9 +32,9 @@ func TestCreateRepoErrorFromGithub(t *testing.T) {
restclient.AddMockup(restclient.Mock{
Url: "https://api.github.com/user/repos",
HttpMethod: http.MethodPost,
BodyText: `{"message": "Requires authentication","documentation_url": "https://developer.github.com/docs"}`,
Response: &http.Response{
StatusCode: http.StatusUnauthorized,
Body: ioutil.NopCloser(strings.NewReader(`{"message": "Requires authentication","documentation_url": "https://developer.github.com/docs"}`)),
},
})
request := repositories.CreateRepoRequest{Name: "testing"}
Expand All @@ -53,9 +51,9 @@ func TestCreateRepoNoError(t *testing.T) {
restclient.AddMockup(restclient.Mock{
Url: "https://api.github.com/user/repos",
HttpMethod: http.MethodPost,
BodyText: `{"id": 123, "name": "testing", "owner": {"login": "federicoleon"}}`,
Response: &http.Response{
StatusCode: http.StatusCreated,
Body: ioutil.NopCloser(strings.NewReader(`{"id": 123, "name": "testing", "owner": {"login": "federicoleon"}}`)),
},
})
request := repositories.CreateRepoRequest{Name: "testing"}
Expand Down Expand Up @@ -89,9 +87,9 @@ func TestCreateRepoConcurrentErrorFromGithub(t *testing.T) {
restclient.AddMockup(restclient.Mock{
Url: "https://api.github.com/user/repos",
HttpMethod: http.MethodPost,
BodyText: `{"message": "Requires authentication","documentation_url": "https://developer.github.com/docs"}`,
Response: &http.Response{
StatusCode: http.StatusUnauthorized,
Body: ioutil.NopCloser(strings.NewReader(`{"message": "Requires authentication","documentation_url": "https://developer.github.com/docs"}`)),
},
})

Expand All @@ -115,9 +113,9 @@ func TestCreateRepoConcurrentNoError(t *testing.T) {
restclient.AddMockup(restclient.Mock{
Url: "https://api.github.com/user/repos",
HttpMethod: http.MethodPost,
BodyText: `{"id": 123, "name": "testing", "owner": {"login": "federicoleon"}}`,
Response: &http.Response{
StatusCode: http.StatusCreated,
Body: ioutil.NopCloser(strings.NewReader(`{"id": 123, "name": "testing", "owner": {"login": "federicoleon"}}`)),
},
})

Expand Down Expand Up @@ -194,9 +192,9 @@ func TestCreateReposOneSuccessOneFail(t *testing.T) {
restclient.AddMockup(restclient.Mock{
Url: "https://api.github.com/user/repos",
HttpMethod: http.MethodPost,
BodyText: `{"id": 123, "name": "testing", "owner": {"login": "federicoleon"}}`,
Response: &http.Response{
StatusCode: http.StatusCreated,
Body: ioutil.NopCloser(strings.NewReader(`{"id": 123, "name": "testing", "owner": {"login": "federicoleon"}}`)),
},
})

Expand Down Expand Up @@ -225,14 +223,14 @@ func TestCreateReposOneSuccessOneFail(t *testing.T) {
}
}

func TestCreateReposRepoAlreadyExistsFailure(t *testing.T) {
func TestCreateReposTwoSuccess(t *testing.T) {
restclient.FlushMockups()
restclient.AddMockup(restclient.Mock{
Url: "https://api.github.com/user/repos",
HttpMethod: http.MethodPost,
BodyText: `{"id": 123, "name": "testing", "owner": {"login": "federicoleon"}}`,
Response: &http.Response{
StatusCode: http.StatusCreated,
Body: ioutil.NopCloser(strings.NewReader(`{"id": 123, "name": "testing", "owner": {"login": "federicoleon"}}`)),
},
})

Expand All @@ -245,18 +243,15 @@ func TestCreateReposRepoAlreadyExistsFailure(t *testing.T) {

assert.Nil(t, err)
assert.NotNil(t, result)
assert.EqualValues(t, http.StatusPartialContent, result.StatusCode)
assert.EqualValues(t, http.StatusCreated, result.StatusCode)
assert.EqualValues(t, 2, len(result.Results))

for _, result := range result.Results {
if result.Error != nil {
assert.EqualValues(t, http.StatusInternalServerError, result.Error.Status())
assert.EqualValues(t, "error when trying to unmarshal github create repo response", result.Error.Message())
continue
}
assert.EqualValues(t, 123, result.Results[0].Response.Id)
assert.EqualValues(t, "testing", result.Results[0].Response.Name)
assert.EqualValues(t, "federicoleon", result.Results[0].Response.Owner)

assert.EqualValues(t, 123, result.Response.Id)
assert.EqualValues(t, "testing", result.Response.Name)
assert.EqualValues(t, "federicoleon", result.Response.Owner)
}
assert.EqualValues(t, 123, result.Results[1].Response.Id)
assert.EqualValues(t, "testing", result.Results[1].Response.Name)
assert.EqualValues(t, "federicoleon", result.Results[1].Response.Owner)
}