Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[test] add mock test for client package #966

Closed
allencloud opened this issue Mar 25, 2018 · 1 comment · Fixed by #1093 or #1457
Closed

[test] add mock test for client package #966

allencloud opened this issue Mar 25, 2018 · 1 comment · Fixed by #1093 or #1457

Comments

@allencloud
Copy link
Collaborator

Ⅰ. Issue Description

For more reliable client package which could be a golang SDK for Pouch developer, we should add more guarantee work on this.

When adding mock test for Pouch's client package, we could refer to PR #965 which could be regarded to be an example.

To be more specific, when doing mock test for API calling function in package client, we should add a mock server function in HTTPClient, then we simulate the action of server in the mock function.

Like:

func TestContainerStart(t *testing.T) {
	expectedURL := "/containers/container_id/start"

	httpClient := newMockClient(func(req *http.Request) (*http.Response, error) {
		if !strings.HasPrefix(req.URL.Path, expectedURL) {
			return nil, fmt.Errorf("Expected URL '%s', got '%s'", expectedURL, req.URL)
		}
		if req.Header.Get("Content-Type") == "application/json" {
			var startConfig interface{}
			if err := json.NewDecoder(req.Body).Decode(&startConfig); err != nil {
				return nil, fmt.Errorf("failed to parse json: %v", err)
			}
		}

		return &http.Response{
			StatusCode: http.StatusOK,
			Body:       ioutil.NopCloser(bytes.NewReader([]byte(""))),
		}, nil
	})

	client := &APIClient{
		HTTPCli: httpClient,
	}

	if err := client.ContainerStart(context.Background(), "container_id", ""); err != nil {
		t.Fatal(err)
	}
}

In that newMockClient(func(req *http.Request) (*http.Response, error) is to simulate the handler of a server. But actually it is in the client side.

So when we call client.ContainerStart, the request will be dealt by newMockClient.

Help Wanted

There are still many API calling functions which misses such kind of mock test. Help wanted for the solving. 👍 🛩 🦁 🍺

@allencloud
Copy link
Collaborator Author

Could you help to check whether this work has been all done by contributors? If not, could you keep to work on this? @ZouRui89

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants