Skip to content

Commit

Permalink
Merge pull request #41 from xushiwei/test
Browse files Browse the repository at this point in the history
mv auth => yap/ytest/auth
  • Loading branch information
xushiwei authored Jan 30, 2024
2 parents 1648a42 + c19a2bb commit 61ea5ad
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 36 deletions.
28 changes: 10 additions & 18 deletions ytest/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,20 @@

package ytest

import "net/http"
import (
"github.com/goplus/yap/ytest/auth"
"github.com/goplus/yap/ytest/auth/token"
)

type tokenAuth struct {
// Type string
Token string
}

type tokenRounderTripper struct {
RoundTripper http.RoundTripper
Token string
}
// -----------------------------------------------------------------------------

func (rt *tokenRounderTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", rt.Token)
return rt.RoundTripper.RoundTrip(req)
func Oauth2(auth string) auth.RTComposer {
return nil
}

func (p *tokenAuth) Compose(rt http.RoundTripper) http.RoundTripper {
return &tokenRounderTripper{
RoundTripper: rt,
Token: p.Token,
}
// Token creates an Authorization by specified token.
func Token(auth string) auth.RTComposer {
return token.New(auth)
}

// -----------------------------------------------------------------------------
26 changes: 26 additions & 0 deletions ytest/auth/auth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package auth

import (
"net/http"
)

// RTComposer represents an abstract of http Authorization objects.
type RTComposer interface {
Compose(base http.RoundTripper) http.RoundTripper
}
57 changes: 57 additions & 0 deletions ytest/auth/token/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package token

import (
"net/http"

"github.com/goplus/yap/ytest/auth"
)

// -----------------------------------------------------------------------------

type tokenRounderTripper struct {
rt http.RoundTripper
token string
}

func (p *tokenRounderTripper) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Set("Authorization", p.token)
return p.rt.RoundTrip(req)
}

// -----------------------------------------------------------------------------

type tokenAuth struct {
token string
}

func (p *tokenAuth) Compose(rt http.RoundTripper) http.RoundTripper {
return &tokenRounderTripper{
rt: rt,
token: p.token,
}
}

// New creates an Authorization by specified token.
func New(token string) auth.RTComposer {
return &tokenAuth{
token: token,
}
}

// -----------------------------------------------------------------------------
12 changes: 0 additions & 12 deletions ytest/classfile.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,3 @@ func (p *App) newRequest(method, url string, body io.Reader) (req *http.Request,
}

// -----------------------------------------------------------------------------

func Oauth2(auth string) RTComposer {
return nil
}

func Token(auth string) RTComposer {
return &tokenAuth{
Token: auth,
}
}

// -----------------------------------------------------------------------------
10 changes: 4 additions & 6 deletions ytest/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ import (
"net/http"
"net/url"
"strings"
)

type RTComposer interface {
Compose(base http.RoundTripper) http.RoundTripper
}
"github.com/goplus/yap/ytest/auth"
)

type RequestBody interface {
io.Reader
Expand All @@ -40,7 +38,7 @@ type Request struct {
method string
url string
header http.Header
auth RTComposer
auth auth.RTComposer
bodyType string
body RequestBody
resp *Response
Expand All @@ -56,7 +54,7 @@ func newRequest(ctx *Case, method, url string) *Request {
}
}

func (p *Request) Auth(auth RTComposer) *Request {
func (p *Request) Auth(auth auth.RTComposer) *Request {
p.auth = auth
return p
}
Expand Down

0 comments on commit 61ea5ad

Please sign in to comment.