Skip to content

Commit

Permalink
final commit, everything working
Browse files Browse the repository at this point in the history
  • Loading branch information
AryanGodara committed Jul 17, 2022
1 parent 04b256c commit 4bb6bcb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 38 deletions.
16 changes: 8 additions & 8 deletions examples/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
func TestMain(m *testing.M) {
fmt.Println("About to start test cases for package 'examples'")

gohttp_mock.StartMockServer()
defer gohttp_mock.StopMockServer()
gohttp_mock.MockupServer.Start()
defer gohttp_mock.MockupServer.Stop()

os.Exit(m.Run())
}
Expand All @@ -25,9 +25,9 @@ func TestGetEndpoint(t *testing.T) {

t.Run("TestErrorFetchingFromGithub", func(t *testing.T) {
// Initialization:
gohttp_mock.DeleteMocks()
gohttp_mock.MockupServer.DeleteMocks()

gohttp_mock.AddMock(gohttp_mock.Mock{
gohttp_mock.MockupServer.AddMock(gohttp_mock.Mock{
Method: http.MethodGet,
Url: "https://api.github.com",
Error: errors.New("timeout getting github endpoints"),
Expand All @@ -52,9 +52,9 @@ func TestGetEndpoint(t *testing.T) {

t.Run("TestErrorUnmarshalResponseBody", func(t *testing.T) {
// Initialization:
gohttp_mock.DeleteMocks()
gohttp_mock.MockupServer.DeleteMocks()

gohttp_mock.AddMock(gohttp_mock.Mock{
gohttp_mock.MockupServer.AddMock(gohttp_mock.Mock{
Method: http.MethodGet,
Url: "https://api.github.com",
ResponseStatusCode: http.StatusOK,
Expand All @@ -79,9 +79,9 @@ func TestGetEndpoint(t *testing.T) {
})
t.Run("TestNoError", func(t *testing.T) {
// Initialization:
gohttp_mock.DeleteMocks()
gohttp_mock.MockupServer.DeleteMocks()

gohttp_mock.AddMock(gohttp_mock.Mock{
gohttp_mock.MockupServer.AddMock(gohttp_mock.Mock{
Method: http.MethodGet,
Url: "https://api.github.com",
ResponseStatusCode: http.StatusOK,
Expand Down
8 changes: 4 additions & 4 deletions examples/post_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (

func TestCreateRepo(t *testing.T) {
t.Run("timeoutFromGithub", func(t *testing.T) {
gohttp_mock.DeleteMocks()
gohttp_mock.AddMock(gohttp_mock.Mock{
gohttp_mock.MockupServer.DeleteMocks()
gohttp_mock.MockupServer.AddMock(gohttp_mock.Mock{
Method: http.MethodPost,
Url: "https://api.github.com/user/repos",
RequestBody: `{"name":"test-repo","private":true}`,
Expand Down Expand Up @@ -42,8 +42,8 @@ func TestCreateRepo(t *testing.T) {
})

t.Run("noError", func(t *testing.T) {
gohttp_mock.DeleteMocks()
gohttp_mock.AddMock(gohttp_mock.Mock{
gohttp_mock.MockupServer.DeleteMocks()
gohttp_mock.MockupServer.AddMock(gohttp_mock.Mock{
Method: http.MethodPost,
Url: "https://api.github.com/user/repos",
RequestBody: `{"name":"test-repo","private":true}`,
Expand Down
4 changes: 2 additions & 2 deletions gohttp/client_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ const (
)

func (c *httpClient) getHttpClient() core.HttpClient {
if gohttp_mock.IsMockServerEnabled() {
return gohttp_mock.GetMockedClient()
if gohttp_mock.MockupServer.IsEnabled() {
return gohttp_mock.MockupServer.GetMockedClient()
}

c.clientOnce.Do(func() { // func() is executed only once, even in concurrent enviornments
Expand Down
2 changes: 1 addition & 1 deletion gohttp_mock/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func (c *httpClientMock) Do(request *http.Request) (*http.Response, error) {

var response http.Response

mock := mockupServer.mocks[mockupServer.getMockKey(request.Method, request.URL.String(), string(body))]
mock := MockupServer.mocks[MockupServer.getMockKey(request.Method, request.URL.String(), string(body))]
if mock != nil {
if mock.Error != nil {
return nil, mock.Error
Expand Down
46 changes: 23 additions & 23 deletions gohttp_mock/mock_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

var (
mockupServer = mockServer{
MockupServer = mockServer{
mocks: make(map[string]*Mock),
enabled: false,
serverMutex: sync.Mutex{},
Expand All @@ -27,39 +27,39 @@ type mockServer struct {
mocks map[string]*Mock
}

func StartMockServer() {
mockupServer.serverMutex.Lock()
defer mockupServer.serverMutex.Unlock()
func (m *mockServer) Start() {
m.serverMutex.Lock()
defer m.serverMutex.Unlock()

mockupServer.enabled = true
m.enabled = true
}

func StopMockServer() {
mockupServer.serverMutex.Lock()
defer mockupServer.serverMutex.Unlock()
func (m *mockServer) Stop() {
m.serverMutex.Lock()
defer m.serverMutex.Unlock()

mockupServer.enabled = false
m.enabled = false
}

func DeleteMocks() {
mockupServer.serverMutex.Lock()
defer mockupServer.serverMutex.Unlock()
func (m *mockServer) DeleteMocks() {
m.serverMutex.Lock()
defer m.serverMutex.Unlock()

mockupServer.mocks = make(map[string]*Mock) // Remove prev map, create a fresh one
m.mocks = make(map[string]*Mock) // Remove prev map, create a fresh one
}

func AddMock(mock Mock) {
mockupServer.serverMutex.Lock()
defer mockupServer.serverMutex.Unlock()
func (m *mockServer) AddMock(mock Mock) {
m.serverMutex.Lock()
defer m.serverMutex.Unlock()

key := mockupServer.getMockKey(mock.Method, mock.Url, mock.RequestBody)
mockupServer.mocks[key] = &mock
key := m.getMockKey(mock.Method, mock.Url, mock.RequestBody)
m.mocks[key] = &mock
}

func (m *mockServer) getMockKey(method, url, body string) string {
hasher := md5.New()

key := method + url + m.cleanBody(body)
key := method + url + MockupServer.cleanBody(body)
hasher.Write([]byte(key))

key = hex.EncodeToString(hasher.Sum(nil))
Expand All @@ -80,10 +80,10 @@ func (m *mockServer) cleanBody(body string) string {
return body
}

func IsMockServerEnabled() bool {
return mockupServer.enabled
func (m *mockServer) IsEnabled() bool {
return m.enabled
}

func GetMockedClient() core.HttpClient {
return mockupServer.httpClient
func (m *mockServer) GetMockedClient() core.HttpClient {
return m.httpClient
}

0 comments on commit 4bb6bcb

Please sign in to comment.