-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from yccodr/feat/api-test
- Loading branch information
Showing
8 changed files
with
412 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package sbi_test | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/andy89923/nf-example/internal/sbi" | ||
"github.com/andy89923/nf-example/pkg/factory" | ||
"github.com/gin-gonic/gin" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
func Test_HTTPSerchSpyFamilyCharacter(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
nfApp := sbi.NewMocknfApp(mockCtrl) | ||
nfApp.EXPECT().Config().Return(&factory.Config{ | ||
Configuration: &factory.Configuration{ | ||
Sbi: &factory.Sbi{ | ||
Port: 8000, | ||
}, | ||
}, | ||
}).AnyTimes() | ||
server := sbi.NewServer(nfApp, "") | ||
|
||
t.Run("No name provided", func(t *testing.T) { | ||
const EXPECTED_STATUS = http.StatusBadRequest | ||
const EXPECTED_BODY = "No name provided" | ||
|
||
httpRecorder := httptest.NewRecorder() | ||
ginCtx, _ := gin.CreateTestContext(httpRecorder) | ||
|
||
var err error | ||
ginCtx.Request, err = http.NewRequest("GET", "/spyfamily", nil) | ||
if err != nil { | ||
t.Errorf("Failed to create request: %s", err) | ||
return | ||
} | ||
|
||
server.HTTPSerchSpyFamilyCharacter(ginCtx) | ||
|
||
if httpRecorder.Code != EXPECTED_STATUS { | ||
t.Errorf("Expected status code %d, got %d", EXPECTED_STATUS, httpRecorder.Code) | ||
} | ||
|
||
if httpRecorder.Body.String() != EXPECTED_BODY { | ||
t.Errorf("Expected body %s, got %s", EXPECTED_BODY, httpRecorder.Body.String()) | ||
} | ||
}) | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package processor_test | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
|
||
nf_context "github.com/andy89923/nf-example/internal/context" | ||
"github.com/andy89923/nf-example/internal/sbi/processor" | ||
"github.com/gin-gonic/gin" | ||
gomock "go.uber.org/mock/gomock" | ||
) | ||
|
||
func Test_FindSpyFamilyCharacterName(t *testing.T) { | ||
mockCtrl := gomock.NewController(t) | ||
processorNf := processor.NewMockProcessorNf(mockCtrl) | ||
processor, err := processor.NewProcessor(processorNf) | ||
if err != nil { | ||
t.Errorf("Failed to create processor: %s", err) | ||
return | ||
} | ||
|
||
t.Run("Find Character That Exists", func(t *testing.T) { | ||
const INPUT_NAME = "Anya" | ||
const EXPECTED_STATUS = 200 | ||
const EXPECTED_BODY = "Character: " + INPUT_NAME + " Forger" | ||
|
||
processorNf.EXPECT().Context().Return(&nf_context.NFContext{ | ||
SpyFamilyData: map[string]string{ | ||
"Anya": "Forger", | ||
}, | ||
}) | ||
|
||
httpRecorder := httptest.NewRecorder() | ||
ginCtx, _ := gin.CreateTestContext(httpRecorder) | ||
processor.FindSpyFamilyCharacterName(ginCtx, INPUT_NAME) | ||
|
||
if httpRecorder.Code != EXPECTED_STATUS { | ||
t.Errorf("Expected status code %d, got %d", EXPECTED_STATUS, httpRecorder.Code) | ||
} | ||
|
||
if httpRecorder.Body.String() != EXPECTED_BODY { | ||
t.Errorf("Expected body %s, got %s", EXPECTED_BODY, httpRecorder.Body.String()) | ||
} | ||
}) | ||
|
||
t.Run("Find Character That Does Not Exist", func(t *testing.T) { | ||
const INPUT_NAME = "Andy" | ||
const EXPECTED_STATUS = 404 | ||
const EXPECTED_BODY = "[" + INPUT_NAME + "] not found in SPYxFAMILY" | ||
|
||
processorNf.EXPECT().Context().Return(&nf_context.NFContext{ | ||
SpyFamilyData: map[string]string{ | ||
"Anya": "Forger", | ||
}, | ||
}) | ||
|
||
httpRecorder := httptest.NewRecorder() | ||
ginCtx, _ := gin.CreateTestContext(httpRecorder) | ||
processor.FindSpyFamilyCharacterName(ginCtx, INPUT_NAME) | ||
|
||
if httpRecorder.Code != EXPECTED_STATUS { | ||
t.Errorf("Expected status code %d, got %d", EXPECTED_STATUS, httpRecorder.Code) | ||
} | ||
|
||
if httpRecorder.Body.String() != EXPECTED_BODY { | ||
t.Errorf("Expected body %s, got %s", EXPECTED_BODY, httpRecorder.Body.String()) | ||
} | ||
}) | ||
} |
Oops, something went wrong.