コード例 #1
0
ファイル: handler.go プロジェクト: helphone/api
func getCountries(w http.ResponseWriter, r *http.Request) {
	query := r.URL.Query()
	languageCode := query.Get("language")
	if languageCode == "" {
		languageCode = "en"
	}

	countryCode, err := getCountryFromLatAndLong(w, r)
	if err != nil {
		return
	}

	var data *model.Response

	if countryCode == "" {
		data, err = manager.GetCountries(languageCode)
	} else {
		data, err = manager.GetCountry(countryCode, languageCode)
	}

	if err != nil {
		fmt.Fprintf(w, "%s", err)
		return
	}

	response, err := json.Marshal(data)
	if err != nil {
		fmt.Fprintf(w, "%s", err)
		return
	}

	renderWithETag(w, r, response)
}
コード例 #2
0
ファイル: helper_test.go プロジェクト: helphone/api
func TestRenderWithETag(t *testing.T) {
	req, w := prepareRequest("test")
	response, _ := manager.GetCountries("en")
	data, _ := json.Marshal(response)

	renderWithETag(w, req, data)

	contentType := w.Header().Get("Content-Type")
	if contentType != "application/json; charset=utf-8" {
		t.Errorf("Content-Type is not good. Receive %s", contentType)
	}

	eTag := w.Header().Get("ETag")
	if eTag == "" {
		t.Error("ETag should be set")
	}

	req, w = prepareRequest("test")
	req.Header.Set("If-None-Match", eTag)

	renderWithETag(w, req, data)

	if w.Code != http.StatusNotModified {
		t.Errorf("The status code of the second request with ETag should be not modified. Got %v", w.Code)
	}

	if len(w.Body.String()) > 0 {
		t.Errorf("The body of a not modified status should be empty. Got %s", w.Body.String())
	}
}