func()

in jupytertestutil/jupytertestutil.go [368:424]


func (m *mockJupyter) handleKernelsRequest(w http.ResponseWriter, r *http.Request, body []byte) {
	if strings.HasPrefix(m.relativePath(r), "/api/kernels/") {
		kernelID := strings.Split(strings.TrimPrefix(m.relativePath(r), "/api/kernels/"), "/")[0]
		switch method := r.Method; method {
		case http.MethodGet:
			if websocket.IsWebSocketUpgrade(r) {
				m.connectToKernel(w, r, kernelID)
				return
			}
			m.getKernel(w, r, kernelID)
			return
		case http.MethodDelete:
			m.deleteKernel(w, r, kernelID)
			return
		default:
			http.Error(w, fmt.Sprintf("Method not supported for path %q", m.relativePath(r)), http.StatusBadRequest)
			return
		}
	}
	switch method := r.Method; method {
	case http.MethodPost:
		var k resources.Kernel
		if err := json.Unmarshal(body, &k); err != nil {
			http.Error(w, fmt.Sprintf("malformed kernel resource: %q, %v", string(body), err), http.StatusBadRequest)
			return
		}
		saved, err := m.insertKernel(&k)
		if err != nil {
			http.Error(w, err.Error(), util.HTTPStatusCode(err))
			return
		}
		resp, err := json.Marshal(saved)
		if err != nil {
			http.Error(w, fmt.Sprintf("failed to marshal kernel: %v", err), http.StatusInternalServerError)
			return
		}
		w.WriteHeader(http.StatusCreated)
		w.Write(resp)
		return
	case http.MethodGet:
		m.mu.Lock()
		defer m.mu.Unlock()
		var kc []*resources.Kernel
		for _, k := range m.kernels {
			kc = append(kc, k)
		}
		resp, err := json.Marshal(kc)
		if err != nil {
			http.Error(w, fmt.Sprintf("failed to marshal kernels: %v", err), http.StatusInternalServerError)
			return
		}
		w.Write(resp)
		return
	default:
		http.Error(w, fmt.Sprintf("Method not supported for path %q", m.relativePath(r)), http.StatusBadRequest)
	}
}