func()

in jupytertestutil/jupytertestutil.go [590:631]


func (m *mockJupyter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	log.Printf("Request to the mock Jupyter server: %+v", r)
	defer func() {
		log.Printf("Response headers for request to %q: %+v", r.URL.Path, w.Header())
	}()
	if m.availableTime.After(time.Now()) {
		http.Error(w, http.StatusText(http.StatusTooEarly), http.StatusTooEarly)
		return
	}
	if !strings.HasPrefix(path.Join("/", r.URL.Path), path.Join("/", m.basePath)) {
		http.NotFound(w, r)
		return
	}

	defer m.recordURL(r.URL.Path)
	if r.Method != http.MethodGet && r.Method != http.MethodHead {
		// Inject artificial latency on mutation requests to simulate real-world performance.
		time.Sleep(m.injectLatency)
	}
	if m.shouldFailRequest(r) {
		w.WriteHeader(http.StatusInternalServerError)
		return
	}

	defer r.Body.Close()
	body, err := io.ReadAll(r.Body)
	if err != nil {
		http.Error(w, fmt.Sprintf("failure reading request body: %v", err), http.StatusInternalServerError)
	}
	collection := strings.Split(strings.TrimPrefix(strings.TrimPrefix(m.relativePath(r), "/api/"), "/"), "/")[0]
	log.Printf("Request to the mock Jupyter server for the %q collection...", collection)
	switch collection {
	case "kernelspecs":
		m.handleKernelspecsRequest(w, r, body)
	case "kernels":
		m.handleKernelsRequest(w, r, body)
	case "sessions":
		m.handleSessionsRequest(w, r, body)
	default:
		http.Error(w, fmt.Sprintf("Method not supported for path %q", m.relativePath(r)), http.StatusBadRequest)
	}
}