func()

in jupytertestutil/jupytertestutil.go [516:573]


func (m *mockJupyter) handleSessionsRequest(w http.ResponseWriter, r *http.Request, body []byte) {
	if strings.HasPrefix(m.relativePath(r), "/api/sessions/") {
		sessionID := strings.TrimPrefix(m.relativePath(r), "/api/sessions/")
		switch method := r.Method; method {
		case http.MethodGet:
			m.getSession(w, r, sessionID)
			return
		case http.MethodDelete:
			m.deleteSession(w, r, sessionID)
			return
		case http.MethodPatch:
			var s resources.Session
			if err := json.Unmarshal(body, &s); err != nil {
				http.Error(w, fmt.Sprintf("malformed session resource: %q, %v", string(body), err), http.StatusBadRequest)
			}
			m.updateSession(w, r, sessionID, &s)
			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 s resources.Session
		if err := json.Unmarshal(body, &s); err != nil {
			http.Error(w, fmt.Sprintf("malformed session resource: %q, %v", string(body), err), http.StatusBadRequest)
		}
		saved, err := m.insertSession(&s)
		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 session: %v", err), http.StatusInternalServerError)
			return
		}
		w.WriteHeader(http.StatusCreated)
		w.Write(resp)
	case http.MethodGet:
		m.mu.Lock()
		defer m.mu.Unlock()
		var sc []*resources.Session
		for _, s := range m.sessions {
			sc = append(sc, s)
		}
		resp, err := json.Marshal(sc)
		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)
	}
}