Skip to content

Commit cc1bc67

Browse files
committed
assert params and body
1 parent e267a3c commit cc1bc67

File tree

1 file changed

+102
-24
lines changed

1 file changed

+102
-24
lines changed

pkg/github/repositories_test.go

+102-24
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,13 @@ func Test_GetFileContents(t *testing.T) {
7070
{
7171
name: "successful file content fetch",
7272
mockedClient: mock.NewMockedHTTPClient(
73-
mock.WithRequestMatch(
73+
mock.WithRequestMatchHandler(
7474
mock.GetReposContentsByOwnerByRepoByPath,
75-
mockFileContent,
75+
expectQueryParams(t, map[string]string{
76+
"ref": "main",
77+
}).andThen(
78+
mockResponse(t, http.StatusOK, mockFileContent),
79+
),
7680
),
7781
),
7882
requestArgs: map[string]interface{}{
@@ -87,9 +91,11 @@ func Test_GetFileContents(t *testing.T) {
8791
{
8892
name: "successful directory content fetch",
8993
mockedClient: mock.NewMockedHTTPClient(
90-
mock.WithRequestMatch(
94+
mock.WithRequestMatchHandler(
9195
mock.GetReposContentsByOwnerByRepoByPath,
92-
mockDirContent,
96+
expectQueryParams(t, map[string]string{}).andThen(
97+
mockResponse(t, http.StatusOK, mockDirContent),
98+
),
9399
),
94100
),
95101
requestArgs: map[string]interface{}{
@@ -352,9 +358,14 @@ func Test_CreateBranch(t *testing.T) {
352358
mock.GetReposGitRefByOwnerByRepoByRef,
353359
mockSourceRef,
354360
),
355-
mock.WithRequestMatch(
361+
mock.WithRequestMatchHandler(
356362
mock.PostReposGitRefsByOwnerByRepo,
357-
mockCreatedRef,
363+
expectRequestBody(t, map[string]interface{}{
364+
"ref": "refs/heads/new-feature",
365+
"sha": "abc123def456",
366+
}).andThen(
367+
mockResponse(t, http.StatusCreated, mockCreatedRef),
368+
),
358369
),
359370
),
360371
requestArgs: map[string]interface{}{
@@ -538,9 +549,15 @@ func Test_ListCommits(t *testing.T) {
538549
{
539550
name: "successful commits fetch with branch",
540551
mockedClient: mock.NewMockedHTTPClient(
541-
mock.WithRequestMatch(
552+
mock.WithRequestMatchHandler(
542553
mock.GetReposCommitsByOwnerByRepo,
543-
mockCommits,
554+
expectQueryParams(t, map[string]string{
555+
"sha": "main",
556+
"page": "1",
557+
"per_page": "30",
558+
}).andThen(
559+
mockResponse(t, http.StatusOK, mockCommits),
560+
),
544561
),
545562
),
546563
requestArgs: map[string]interface{}{
@@ -554,9 +571,14 @@ func Test_ListCommits(t *testing.T) {
554571
{
555572
name: "successful commits fetch with pagination",
556573
mockedClient: mock.NewMockedHTTPClient(
557-
mock.WithRequestMatch(
574+
mock.WithRequestMatchHandler(
558575
mock.GetReposCommitsByOwnerByRepo,
559-
mockCommits,
576+
expectQueryParams(t, map[string]string{
577+
"page": "2",
578+
"per_page": "10",
579+
}).andThen(
580+
mockResponse(t, http.StatusOK, mockCommits),
581+
),
560582
),
561583
),
562584
requestArgs: map[string]interface{}{
@@ -676,9 +698,15 @@ func Test_CreateOrUpdateFile(t *testing.T) {
676698
{
677699
name: "successful file creation",
678700
mockedClient: mock.NewMockedHTTPClient(
679-
mock.WithRequestMatch(
701+
mock.WithRequestMatchHandler(
680702
mock.PutReposContentsByOwnerByRepoByPath,
681-
mockFileResponse,
703+
expectRequestBody(t, map[string]interface{}{
704+
"message": "Add example file",
705+
"content": "IyBFeGFtcGxlCgpUaGlzIGlzIGFuIGV4YW1wbGUgZmlsZS4=", // Base64 encoded content
706+
"branch": "main",
707+
}).andThen(
708+
mockResponse(t, http.StatusOK, mockFileResponse),
709+
),
682710
),
683711
),
684712
requestArgs: map[string]interface{}{
@@ -695,9 +723,16 @@ func Test_CreateOrUpdateFile(t *testing.T) {
695723
{
696724
name: "successful file update with SHA",
697725
mockedClient: mock.NewMockedHTTPClient(
698-
mock.WithRequestMatch(
726+
mock.WithRequestMatchHandler(
699727
mock.PutReposContentsByOwnerByRepoByPath,
700-
mockFileResponse,
728+
expectRequestBody(t, map[string]interface{}{
729+
"message": "Update example file",
730+
"content": "IyBVcGRhdGVkIEV4YW1wbGUKClRoaXMgZmlsZSBoYXMgYmVlbiB1cGRhdGVkLg==", // Base64 encoded content
731+
"branch": "main",
732+
"sha": "abc123def456",
733+
}).andThen(
734+
mockResponse(t, http.StatusOK, mockFileResponse),
735+
),
701736
),
702737
),
703738
requestArgs: map[string]interface{}{
@@ -819,7 +854,14 @@ func Test_CreateRepository(t *testing.T) {
819854
Pattern: "/user/repos",
820855
Method: "POST",
821856
},
822-
mockResponse(t, http.StatusCreated, mockRepo),
857+
expectRequestBody(t, map[string]interface{}{
858+
"name": "test-repo",
859+
"description": "Test repository",
860+
"private": true,
861+
"auto_init": true,
862+
}).andThen(
863+
mockResponse(t, http.StatusCreated, mockRepo),
864+
),
823865
),
824866
),
825867
requestArgs: map[string]interface{}{
@@ -839,7 +881,14 @@ func Test_CreateRepository(t *testing.T) {
839881
Pattern: "/user/repos",
840882
Method: "POST",
841883
},
842-
mockResponse(t, http.StatusCreated, mockRepo),
884+
expectRequestBody(t, map[string]interface{}{
885+
"name": "test-repo",
886+
"auto_init": false,
887+
"description": "",
888+
"private": false,
889+
}).andThen(
890+
mockResponse(t, http.StatusCreated, mockRepo),
891+
),
843892
),
844893
),
845894
requestArgs: map[string]interface{}{
@@ -980,19 +1029,48 @@ func Test_PushFiles(t *testing.T) {
9801029
mockCommit,
9811030
),
9821031
// Create tree
983-
mock.WithRequestMatch(
1032+
mock.WithRequestMatchHandler(
9841033
mock.PostReposGitTreesByOwnerByRepo,
985-
mockTree,
1034+
expectRequestBody(t, map[string]interface{}{
1035+
"base_tree": "def456",
1036+
"tree": []interface{}{
1037+
map[string]interface{}{
1038+
"path": "README.md",
1039+
"mode": "100644",
1040+
"type": "blob",
1041+
"content": "# Updated README\n\nThis is an updated README file.",
1042+
},
1043+
map[string]interface{}{
1044+
"path": "docs/example.md",
1045+
"mode": "100644",
1046+
"type": "blob",
1047+
"content": "# Example\n\nThis is an example file.",
1048+
},
1049+
},
1050+
}).andThen(
1051+
mockResponse(t, http.StatusCreated, mockTree),
1052+
),
9861053
),
9871054
// Create commit
988-
mock.WithRequestMatch(
1055+
mock.WithRequestMatchHandler(
9891056
mock.PostReposGitCommitsByOwnerByRepo,
990-
mockNewCommit,
1057+
expectRequestBody(t, map[string]interface{}{
1058+
"message": "Update multiple files",
1059+
"tree": "ghi789",
1060+
"parents": []interface{}{"abc123"},
1061+
}).andThen(
1062+
mockResponse(t, http.StatusCreated, mockNewCommit),
1063+
),
9911064
),
9921065
// Update reference
993-
mock.WithRequestMatch(
1066+
mock.WithRequestMatchHandler(
9941067
mock.PatchReposGitRefsByOwnerByRepoByRef,
995-
mockUpdatedRef,
1068+
expectRequestBody(t, map[string]interface{}{
1069+
"sha": "jkl012",
1070+
"force": false,
1071+
}).andThen(
1072+
mockResponse(t, http.StatusOK, mockUpdatedRef),
1073+
),
9961074
),
9971075
),
9981076
requestArgs: map[string]interface{}{
@@ -1015,9 +1093,9 @@ func Test_PushFiles(t *testing.T) {
10151093
expectedRef: mockUpdatedRef,
10161094
},
10171095
{
1018-
name: "fails when files parameter is invalid",
1096+
name: "fails when files parameter is invalid",
10191097
mockedClient: mock.NewMockedHTTPClient(
1020-
// No requests expected
1098+
// No requests expected
10211099
),
10221100
requestArgs: map[string]interface{}{
10231101
"owner": "owner",

0 commit comments

Comments
 (0)