如何测试服务器的Oauth2.0资源

huangapple go评论132阅读模式
英文:

How to test a Oauth2.0 resource of server

问题

我想编写一个测试来验证正确的文档是否可以访问第三方服务器的Oauth2.0,我应该如何完成伪代码?

import (
	"net/http"
	"net/http/httptest"
	"net/url"
	"strings"
	"testing"
)

func testAuthServer(t *testing.T) {
	form := url.Values{}
	form.Set(...)

	r := httptest.NewRequest(http.MethodPost, authUrl, strings.NewReader(form.Encode()))
	r.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	w := httptest.NewRecorder()

	// 测试认证服务器

	if w.Code != http.StatusOK {
		...
	}
}

请注意,这只是一个伪代码示例,你需要根据你的具体情况进行适当的修改和完善。

英文:

I want to code the test for validate the right document to reach to the Oauth2.0 of third party server, how should i complete the pseudocode?

import (
	"net/http"
	"net/http/httptest"
}

func testAuthServer(t *testing.T) {
	form := url.Values{}
	form.Set(...)

	r := httptest.NewRequest(http.MethodPost, authUrl, strings.NewReader(form.Encode()))
	r.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	w := httptest.NewRecorder()

    // test the auth server

	if w.Code != http.StatusOK {
        ...
    }
}

答案1

得分: 1

你可以依赖第三方库来模拟资源。你可以查看 gock

func TestServer(t *testing.T) {
	defer gock.Off()

	authURL := "http://third-party-resource.com"
	form := url.Values{}
	form.Add("foo", "bar")

	// 创建第三方资源的模拟。我们断言代码会使用POST请求调用该资源,并将请求体设置为"foo=bar"
	gock.New(authURL).
		Post("/").
		BodyString("foo=bar").
		Reply(200)

	r, err := http.NewRequest(http.MethodPost, authURL, strings.NewReader(form.Encode()))
	if err != nil {
		t.Fatal(err)
	}

	r.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	c := http.Client{}
	_, err = c.Do(r)
	if err != nil {
		t.Fatal(err)
	}

	if !gock.IsDone() {
		// 模拟未被调用。
		t.Fatal(gock.GetUnmatchedRequests())
	}
}
英文:

You can rely on a third party library to mock the resource. You can take a look at gock.

func TestServer(t *testing.T) {
	defer gock.Off()

	authURL := "http://third-party-resource.com"
	form := url.Values{}
	form.Add("foo", "bar")

	// Create the mock of the third-party resource. We assert that the code
	// calls the resource with a POST request with the body set to "foo=bar"
	gock.New(authURL).
		Post("/").
		BodyString("foo=bar").
		Reply(200)

	r, err := http.NewRequest(http.MethodPost, authURL, strings.NewReader(form.Encode()))
	if err != nil {
		t.Fatal(err)
	}

	r.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	c := http.Client{}
	_, err = c.Do(r)
	if err != nil {
		t.Fatal(err)
	}

	if !gock.IsDone() {
		// The mock has not been called.
		t.Fatal(gock.GetUnmatchedRequests())
	}
}

答案2

得分: 0

最后我使用普通的HTTP客户端解决了这个问题。

import (
    "net/http"
}

func testAuthServer(t *testing.T) {
    form := url.Values{}
    form.Set(...)

    authReq := http.NewRequest(http.MethodPost, authUrl, strings.NewReader(form.Encode()))
    authReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")

    authClient, _ := http.Client{}
    authResp, _ := authClient.Do(authReq)

    if authResp.Code != http.StatusOK {
        ...
    }
}
英文:

Finally i use the normal http client to resolve this problem.

import (
    "net/http"
}

func testAuthServer(t *testing.T) {
    form := url.Values{}
    form.Set(...)

    authReq := http.NewRequest(http.MethodPost, authUrl, strings.NewReader(form.Encode()))
    authReq.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	authClient, _ := http.Client{}
	authResp, _ := authClient.Do(authReq)

    if authResp.Code != http.StatusOK {
        ...
    }
}

huangapple
  • 本文由 发表于 2021年9月9日 18:07:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/69116263.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定