Skip to content

Commit

Permalink
perf(test): optimize code (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
flc1125 authored Oct 23, 2024
1 parent 5c28406 commit a75a420
Showing 1 changed file with 25 additions and 25 deletions.
50 changes: 25 additions & 25 deletions dispatcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ func testDispatcherContext(ctx context.Context, t *testing.T) {
}

func TestDispatcher_Dispatch(t *testing.T) {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
dispatcher := NewDispatcher(
RegisterListeners(&testListener{t: t}),
)
dispatcher.RegisterListeners(&testListener{t: t})
dispatcher := NewDispatcher(
RegisterListeners(&testListener{t: t}),
)
dispatcher.RegisterListeners(&testListener{t: t})

srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodPost, r.Method)
assert.Equal(t, "/webhook", r.URL.Path)
assert.NoError(t,
Expand All @@ -47,31 +47,31 @@ func TestDispatcher_Dispatch(t *testing.T) {
tests := []struct {
name string
eventType gitlab.EventType
body []byte
filepath string
}{
{"build", gitlab.EventTypeBuild, loadFixture("testdata/webhooks/build.json")}, //nolint:lll
{"commit comment", gitlab.EventTypeNote, loadFixture("testdata/webhooks/note_commit.json")}, //nolint:lll
{"deployment", gitlab.EventTypeDeployment, loadFixture("testdata/webhooks/deployment.json")}, //nolint:lll
{"feature flag", gitlab.EventTypeFeatureFlag, loadFixture("testdata/webhooks/feature_flag.json")}, //nolint:lll
{"group resource access token", gitlab.EventTypeResourceAccessToken, loadFixture("testdata/webhooks/resource_access_token_group.json")}, //nolint:lll
{"issue comment", gitlab.EventTypeNote, loadFixture("testdata/webhooks/note_issue.json")}, //nolint:lll
{"issue", gitlab.EventTypeIssue, loadFixture("testdata/webhooks/issue.json")}, //nolint:lll
{"job", gitlab.EventTypeJob, loadFixture("testdata/webhooks/job.json")},
{"member", gitlab.EventTypeMember, loadFixture("testdata/webhooks/member.json")},
{"merge comment", gitlab.EventTypeNote, loadFixture("testdata/webhooks/note_merge_request.json")}, //nolint:lll
{"merge", gitlab.EventTypeMergeRequest, loadFixture("testdata/webhooks/merge_request.json")}, //nolint:lll
{"pipeline", gitlab.EventTypePipeline, loadFixture("testdata/webhooks/pipeline.json")}, //nolint:lll
{"push", gitlab.EventTypePush, loadFixture("testdata/webhooks/push.json")},
{"release", gitlab.EventTypeRelease, loadFixture("testdata/webhooks/release.json")}, //nolint:lll
{"snippet comment", gitlab.EventTypeNote, loadFixture("testdata/webhooks/note_snippet.json")}, //nolint:lll
{"subgroup", gitlab.EventTypeSubGroup, loadFixture("testdata/webhooks/subgroup.json")}, //nolint:lll
{"tag", gitlab.EventTypeTagPush, loadFixture("testdata/webhooks/tag_push.json")},
{"wiki page", gitlab.EventTypeWikiPage, loadFixture("testdata/webhooks/wiki_page.json")}, //nolint:lll
{"build", gitlab.EventTypeBuild, "testdata/webhooks/build.json"}, //nolint:lll
{"commit comment", gitlab.EventTypeNote, "testdata/webhooks/note_commit.json"}, //nolint:lll
{"deployment", gitlab.EventTypeDeployment, "testdata/webhooks/deployment.json"}, //nolint:lll
{"feature flag", gitlab.EventTypeFeatureFlag, "testdata/webhooks/feature_flag.json"}, //nolint:lll
{"group resource access token", gitlab.EventTypeResourceAccessToken, "testdata/webhooks/resource_access_token_group.json"}, //nolint:lll
{"issue comment", gitlab.EventTypeNote, "testdata/webhooks/note_issue.json"}, //nolint:lll
{"issue", gitlab.EventTypeIssue, "testdata/webhooks/issue.json"}, //nolint:lll
{"job", gitlab.EventTypeJob, "testdata/webhooks/job.json"},
{"member", gitlab.EventTypeMember, "testdata/webhooks/member.json"},
{"merge comment", gitlab.EventTypeNote, "testdata/webhooks/note_merge_request.json"}, //nolint:lll
{"merge", gitlab.EventTypeMergeRequest, "testdata/webhooks/merge_request.json"}, //nolint:lll
{"pipeline", gitlab.EventTypePipeline, "testdata/webhooks/pipeline.json"}, //nolint:lll
{"push", gitlab.EventTypePush, "testdata/webhooks/push.json"},
{"release", gitlab.EventTypeRelease, "testdata/webhooks/release.json"}, //nolint:lll
{"snippet comment", gitlab.EventTypeNote, "testdata/webhooks/note_snippet.json"}, //nolint:lll
{"subgroup", gitlab.EventTypeSubGroup, "testdata/webhooks/subgroup.json"}, //nolint:lll
{"tag", gitlab.EventTypeTagPush, "testdata/webhooks/tag_push.json"},
{"wiki page", gitlab.EventTypeWikiPage, "testdata/webhooks/wiki_page.json"}, //nolint:lll
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
req, err := http.NewRequest(http.MethodPost, srv.URL+"/webhook", bytes.NewReader(tt.body))
req, err := http.NewRequest(http.MethodPost, srv.URL+"/webhook", bytes.NewReader(loadFixture(tt.filepath)))
assert.NoError(t, err)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("X-Gitlab-Event", string(tt.eventType))
Expand Down

0 comments on commit a75a420

Please sign in to comment.