Skip to content

Commit b016605

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

File tree

1 file changed

+99
-24
lines changed

1 file changed

+99
-24
lines changed

pkg/github/repositories_test.go

Lines changed: 99 additions & 24 deletions
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,11 @@ 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+
}).andThen(
887+
mockResponse(t, http.StatusCreated, mockRepo),
888+
),
843889
),
844890
),
845891
requestArgs: map[string]interface{}{
@@ -980,19 +1026,48 @@ func Test_PushFiles(t *testing.T) {
9801026
mockCommit,
9811027
),
9821028
// Create tree
983-
mock.WithRequestMatch(
1029+
mock.WithRequestMatchHandler(
9841030
mock.PostReposGitTreesByOwnerByRepo,
985-
mockTree,
1031+
expectRequestBody(t, map[string]interface{}{
1032+
"base_tree": "def456",
1033+
"tree": []interface{}{
1034+
map[string]interface{}{
1035+
"path": "README.md",
1036+
"mode": "100644",
1037+
"type": "blob",
1038+
"content": "# Updated README\n\nThis is an updated README file.",
1039+
},
1040+
map[string]interface{}{
1041+
"path": "docs/example.md",
1042+
"mode": "100644",
1043+
"type": "blob",
1044+
"content": "# Example\n\nThis is an example file.",
1045+
},
1046+
},
1047+
}).andThen(
1048+
mockResponse(t, http.StatusCreated, mockTree),
1049+
),
9861050
),
9871051
// Create commit
988-
mock.WithRequestMatch(
1052+
mock.WithRequestMatchHandler(
9891053
mock.PostReposGitCommitsByOwnerByRepo,
990-
mockNewCommit,
1054+
expectRequestBody(t, map[string]interface{}{
1055+
"message": "Update multiple files",
1056+
"tree": "ghi789",
1057+
"parents": []interface{}{"abc123"},
1058+
}).andThen(
1059+
mockResponse(t, http.StatusCreated, mockNewCommit),
1060+
),
9911061
),
9921062
// Update reference
993-
mock.WithRequestMatch(
1063+
mock.WithRequestMatchHandler(
9941064
mock.PatchReposGitRefsByOwnerByRepoByRef,
995-
mockUpdatedRef,
1065+
expectRequestBody(t, map[string]interface{}{
1066+
"sha": "jkl012",
1067+
"force": false,
1068+
}).andThen(
1069+
mockResponse(t, http.StatusOK, mockUpdatedRef),
1070+
),
9961071
),
9971072
),
9981073
requestArgs: map[string]interface{}{
@@ -1015,9 +1090,9 @@ func Test_PushFiles(t *testing.T) {
10151090
expectedRef: mockUpdatedRef,
10161091
},
10171092
{
1018-
name: "fails when files parameter is invalid",
1093+
name: "fails when files parameter is invalid",
10191094
mockedClient: mock.NewMockedHTTPClient(
1020-
// No requests expected
1095+
// No requests expected
10211096
),
10221097
requestArgs: map[string]interface{}{
10231098
"owner": "owner",

0 commit comments

Comments
 (0)