GoDasturchi
HTTP Testing with httptest

HTTP Testing with httptest

httptest bilan HTTP'ni sinash

Shu kurs davomida httptest paketini ko'p ishlatdik — endi uning ikki asosiy vositasini aniq tushunib olamiz: httptest.NewRequest (sohta so'rov yaratish) va httptest.NewRecorder (javobni "yozib olish").

example.go
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
)

func healthHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	fmt.Fprint(w, `{"status":"ok"}`)
}

func main() {
	req := httptest.NewRequest("GET", "/health", nil)
	rec := httptest.NewRecorder()

	healthHandler(rec, req)

	fmt.Println(rec.Code)
	fmt.Println(rec.Header().Get("Content-Type"))
	fmt.Println(rec.Body.String())
}

httptest.NewRequest(metod, yo'l, tana) haqiqiy tarmoq ulanishisiz *http.Request yasaydi. httptest.NewRecorder() esa http.ResponseWriterning "soxta" versiyasi — handler unga yozgan hammasini (status, sarlavhalar, tana) keyinroq tekshirish uchun saqlab qoladi: rec.Code, rec.Header(), rec.Body. Bu — nima uchun HTTP handler'larni haqiqiy server ochmasdan, to'g'ridan-to'g'ri funksiya sifatida sinash mumkinligining siri.

>_ Exercise

Handler'ning uchta jihatini (status, sarlavha, tana) tekshiring.

  • versionHandler'ni httptest.NewRequest/NewRecorder bilan chaqiring
  • status kodini, Content-Type sarlavhasini va tanani chop eting

Stuck? Reveal a hint to help you.

Hints (0/3)

Key Takeaway

Key Takeaway:

httptest.NewRequest + httptest.NewRecorder — HTTP handler'larni haqiqiy server yoki tarmoqsiz, to'g'ridan-to'g'ri sinashning standart usuli.

NEXT UP

Query Parameters and Forms

OUTPUT

$ go run main.go
Kodingizni ishga tushiring

HTTP Testing with httptest

httptest bilan HTTP'ni sinash

Shu kurs davomida httptest paketini ko'p ishlatdik — endi uning ikki asosiy vositasini aniq tushunib olamiz: httptest.NewRequest (sohta so'rov yaratish) va httptest.NewRecorder (javobni "yozib olish").

example.go
package main

import (
	"fmt"
	"net/http"
	"net/http/httptest"
)

func healthHandler(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	w.WriteHeader(http.StatusOK)
	fmt.Fprint(w, `{"status":"ok"}`)
}

func main() {
	req := httptest.NewRequest("GET", "/health", nil)
	rec := httptest.NewRecorder()

	healthHandler(rec, req)

	fmt.Println(rec.Code)
	fmt.Println(rec.Header().Get("Content-Type"))
	fmt.Println(rec.Body.String())
}

httptest.NewRequest(metod, yo'l, tana) haqiqiy tarmoq ulanishisiz *http.Request yasaydi. httptest.NewRecorder() esa http.ResponseWriterning "soxta" versiyasi — handler unga yozgan hammasini (status, sarlavhalar, tana) keyinroq tekshirish uchun saqlab qoladi: rec.Code, rec.Header(), rec.Body. Bu — nima uchun HTTP handler'larni haqiqiy server ochmasdan, to'g'ridan-to'g'ri funksiya sifatida sinash mumkinligining siri.

>_ Exercise

Handler'ning uchta jihatini (status, sarlavha, tana) tekshiring.

  • versionHandler'ni httptest.NewRequest/NewRecorder bilan chaqiring
  • status kodini, Content-Type sarlavhasini va tanani chop eting

Stuck? Reveal a hint to help you.

Hints (0/3)

Key Takeaway

Key Takeaway:

httptest.NewRequest + httptest.NewRecorder — HTTP handler'larni haqiqiy server yoki tarmoqsiz, to'g'ridan-to'g'ri sinashning standart usuli.

NEXT UP

Query Parameters and Forms