@@ -21,111 +21,116 @@ const (
2121`
2222)
2323
24- type handler struct {
24+ type Handler struct {
2525 Status int
2626 Body string
2727}
2828
29- func (h * handler ) ServeHTTP (w http.ResponseWriter , req * http.Request ) {
29+ func (h * Handler ) ServeHTTP (w http.ResponseWriter , req * http.Request ) {
3030 w .WriteHeader (h .Status )
3131 fmt .Fprintf (w , h .Body )
3232}
3333
34+ type ServerConfig struct {
35+ * FixedResponses
36+ }
37+
3438// NewServer creates a fake server for running unit tests
3539func NewServer (opts ... Option ) * httptest.Server {
36- var responses FixedResponses
40+ config := ServerConfig {
41+ FixedResponses : & FixedResponses {},
42+ }
3743
3844 for _ , o := range opts {
39- o (& responses )
45+ o (& config )
4046 }
4147
42- routes := map [string ]handler {
48+ routes := map [string ]* Handler {
4349 // For CreateRegistrationToken
44- "/repos/test/valid/actions/runners/registration-token" : handler {
50+ "/repos/test/valid/actions/runners/registration-token" : & Handler {
4551 Status : http .StatusCreated ,
4652 Body : fmt .Sprintf ("{\" token\" : \" %s\" , \" expires_at\" : \" %s\" }" , RegistrationToken , time .Now ().Add (time .Hour * 1 ).Format (time .RFC3339 )),
4753 },
48- "/repos/test/invalid/actions/runners/registration-token" : handler {
54+ "/repos/test/invalid/actions/runners/registration-token" : & Handler {
4955 Status : http .StatusOK ,
5056 Body : fmt .Sprintf ("{\" token\" : \" %s\" , \" expires_at\" : \" %s\" }" , RegistrationToken , time .Now ().Add (time .Hour * 1 ).Format (time .RFC3339 )),
5157 },
52- "/repos/test/error/actions/runners/registration-token" : handler {
58+ "/repos/test/error/actions/runners/registration-token" : & Handler {
5359 Status : http .StatusBadRequest ,
5460 Body : "" ,
5561 },
56- "/orgs/test/actions/runners/registration-token" : handler {
62+ "/orgs/test/actions/runners/registration-token" : & Handler {
5763 Status : http .StatusCreated ,
5864 Body : fmt .Sprintf ("{\" token\" : \" %s\" , \" expires_at\" : \" %s\" }" , RegistrationToken , time .Now ().Add (time .Hour * 1 ).Format (time .RFC3339 )),
5965 },
60- "/orgs/invalid/actions/runners/registration-token" : handler {
66+ "/orgs/invalid/actions/runners/registration-token" : & Handler {
6167 Status : http .StatusOK ,
6268 Body : fmt .Sprintf ("{\" token\" : \" %s\" , \" expires_at\" : \" %s\" }" , RegistrationToken , time .Now ().Add (time .Hour * 1 ).Format (time .RFC3339 )),
6369 },
64- "/orgs/error/actions/runners/registration-token" : handler {
70+ "/orgs/error/actions/runners/registration-token" : & Handler {
6571 Status : http .StatusBadRequest ,
6672 Body : "" ,
6773 },
6874
6975 // For ListRunners
70- "/repos/test/valid/actions/runners" : handler {
76+ "/repos/test/valid/actions/runners" : & Handler {
7177 Status : http .StatusOK ,
7278 Body : RunnersListBody ,
7379 },
74- "/repos/test/invalid/actions/runners" : handler {
80+ "/repos/test/invalid/actions/runners" : & Handler {
7581 Status : http .StatusNoContent ,
7682 Body : "" ,
7783 },
78- "/repos/test/error/actions/runners" : handler {
84+ "/repos/test/error/actions/runners" : & Handler {
7985 Status : http .StatusBadRequest ,
8086 Body : "" ,
8187 },
82- "/orgs/test/actions/runners" : handler {
88+ "/orgs/test/actions/runners" : & Handler {
8389 Status : http .StatusOK ,
8490 Body : RunnersListBody ,
8591 },
86- "/orgs/invalid/actions/runners" : handler {
92+ "/orgs/invalid/actions/runners" : & Handler {
8793 Status : http .StatusNoContent ,
8894 Body : "" ,
8995 },
90- "/orgs/error/actions/runners" : handler {
96+ "/orgs/error/actions/runners" : & Handler {
9197 Status : http .StatusBadRequest ,
9298 Body : "" ,
9399 },
94100
95101 // For RemoveRunner
96- "/repos/test/valid/actions/runners/1" : handler {
102+ "/repos/test/valid/actions/runners/1" : & Handler {
97103 Status : http .StatusNoContent ,
98104 Body : "" ,
99105 },
100- "/repos/test/invalid/actions/runners/1" : handler {
106+ "/repos/test/invalid/actions/runners/1" : & Handler {
101107 Status : http .StatusOK ,
102108 Body : "" ,
103109 },
104- "/repos/test/error/actions/runners/1" : handler {
110+ "/repos/test/error/actions/runners/1" : & Handler {
105111 Status : http .StatusBadRequest ,
106112 Body : "" ,
107113 },
108- "/orgs/test/actions/runners/1" : handler {
114+ "/orgs/test/actions/runners/1" : & Handler {
109115 Status : http .StatusNoContent ,
110116 Body : "" ,
111117 },
112- "/orgs/invalid/actions/runners/1" : handler {
118+ "/orgs/invalid/actions/runners/1" : & Handler {
113119 Status : http .StatusOK ,
114120 Body : "" ,
115121 },
116- "/orgs/error/actions/runners/1" : handler {
122+ "/orgs/error/actions/runners/1" : & Handler {
117123 Status : http .StatusBadRequest ,
118124 Body : "" ,
119125 },
120126
121127 // For auto-scaling based on the number of queued(pending) workflow runs
122- "/repos/test/valid/actions/runs" : responses . listRepositoryWorkflowRuns . handler () ,
128+ "/repos/test/valid/actions/runs" : config . FixedResponses . ListRepositoryWorkflowRuns ,
123129 }
124130
125131 mux := http .NewServeMux ()
126132 for path , handler := range routes {
127- h := handler
128- mux .Handle (path , & h )
133+ mux .Handle (path , handler )
129134 }
130135
131136 return httptest .NewServer (mux )
0 commit comments