コード例 #1
0
ファイル: mockconf_test.go プロジェクト: skiarn/mad-mock
func TestReadResponse_ShouldBuildMockFromResponse(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Content-Type", "RandomContentType")
		w.WriteHeader(418)
		fmt.Fprintln(w, `body with some random data`)
	}))
	defer server.Close()

	// Make a transport that reroutes all traffic to the example server
	transport := &http.Transport{
		Proxy: func(req *http.Request) (*url.URL, error) {
			return url.Parse(server.URL)
		},
	}

	// Make a http.Client with the transport
	httpClient := &http.Client{Transport: transport}

	resp, err := httpClient.Get("http://google.se/test/get")
	if err != nil {
		t.Errorf("Error occured unexpectedly with error: %s", err)
	}

	//Test
	m := model.ReadResponse(resp)
	expectedURI := "/test/get"
	expectedMethod := "GET"
	expectedStatus := 418
	expectedContentType := "RandomContentType"
	if m.URI != expectedURI {
		t.Errorf("Expected: %v but got: %v", expectedURI, m.URI)
	}
	if m.Method != expectedMethod {
		t.Errorf("Expected: %v but got: %v", expectedMethod, m.Method)
	}
	if m.StatusCode != expectedStatus {
		t.Errorf("Expected: %v but got: %v", expectedStatus, m.StatusCode)
	}
	if m.ContentType != expectedContentType {
		t.Errorf("Expected: %v but got: %v", expectedContentType, m.ContentType)
	}
	//verify headers.
	for k, v := range resp.Header {
		for _, vv := range v {
			if m.Header[k] == "" {
				t.Errorf("Expected header: %v with value %v but got %v.", k, vv, m.Header[k])
			}
		}
	}
}
コード例 #2
0
ファイル: mockhandler.go プロジェクト: skiarn/mad-mock
func (h *Mockhandler) requestInfo(w http.ResponseWriter, r *http.Request) (*model.MockConf, error) {
	targetURL := h.BuildTargetRequestURL(r)
	response, err := h.Client.RequestTargetInfo(targetURL, w, r)
	if err != nil {
		return nil, err
	}

	defer response.Body.Close()
	contents, err := ioutil.ReadAll(response.Body)
	if err != nil {
		return nil, err
	}
	c := model.ReadResponse(response)

	err = h.Fs.WriteMock(*c, contents, h.DataDirPath)
	if err != nil {
		return nil, err
	}

	return c, nil
}