MCPcopy
hub / github.com/cli/cli / createFakeCreateEndpointServer

Function createFakeCreateEndpointServer

internal/codespaces/api/api_test.go:75–133  ·  view source on GitHub ↗
(t *testing.T, wantStatus int)

Source from the content-addressed store, hash-verified

73}
74
75func createFakeCreateEndpointServer(t *testing.T, wantStatus int) *httptest.Server {
76 t.Helper()
77 return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
78 // create endpoint
79 if r.URL.Path == "/user/codespaces" {
80 body := r.Body
81 if body == nil {
82 t.Fatal("No body")
83 }
84 defer body.Close()
85
86 var params startCreateRequest
87 err := json.NewDecoder(body).Decode(&params)
88 if err != nil {
89 t.Fatal("error:", err)
90 }
91
92 if params.RepositoryID != 1 {
93 t.Fatal("Expected RepositoryID to be 1. Got: ", params.RepositoryID)
94 }
95
96 if params.IdleTimeoutMinutes != 10 {
97 t.Fatal("Expected IdleTimeoutMinutes to be 10. Got: ", params.IdleTimeoutMinutes)
98 }
99
100 if *params.RetentionPeriodMinutes != 0 {
101 t.Fatal("Expected RetentionPeriodMinutes to be 0. Got: ", *params.RetentionPeriodMinutes)
102 }
103
104 response := Codespace{
105 Name: "codespace-1",
106 DisplayName: params.DisplayName,
107 }
108
109 if wantStatus == 0 {
110 wantStatus = http.StatusCreated
111 }
112
113 w.WriteHeader(wantStatus)
114 enc := json.NewEncoder(w)
115 _ = enc.Encode(&response)
116 return
117 }
118
119 // get endpoint hit for testing pending status
120 if r.URL.Path == "/user/codespaces/codespace-1" {
121 response := Codespace{
122 Name: "codespace-1",
123 State: CodespaceStateAvailable,
124 }
125 w.WriteHeader(http.StatusOK)
126 enc := json.NewEncoder(w)
127 _ = enc.Encode(&response)
128 return
129 }
130
131 t.Fatal("Incorrect path")
132 }))

Callers 3

TestCreateCodespacesFunction · 0.85

Calls 2

HelperMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected