删除过时的测试用例代码, 切换uuid实现
This commit is contained in:
@@ -1,9 +1,8 @@
|
||||
package discovery
|
||||
|
||||
import (
|
||||
"github.com/google/uuid"
|
||||
"sync"
|
||||
|
||||
"github.com/go-basic/uuid"
|
||||
)
|
||||
|
||||
type app struct {
|
||||
@@ -58,7 +57,7 @@ func NewApp(checker IHealthChecker, container IAppContainer, attrs Attrs, nodes
|
||||
nodes: nodes,
|
||||
locker: sync.RWMutex{},
|
||||
healthChecker: checker,
|
||||
id: uuid.New(),
|
||||
id: uuid.NewString(),
|
||||
container: container,
|
||||
}
|
||||
}
|
||||
|
||||
4
go.mod
4
go.mod
@@ -7,14 +7,13 @@ require (
|
||||
github.com/coocood/freecache v1.2.2
|
||||
github.com/eolinker/eosc v0.7.1
|
||||
github.com/fasthttp/websocket v1.5.0
|
||||
github.com/go-basic/uuid v1.0.0
|
||||
github.com/go-redis/redis/v8 v8.11.5
|
||||
github.com/google/uuid v1.3.0
|
||||
github.com/hashicorp/consul/api v1.9.1
|
||||
github.com/nsqio/go-nsq v1.1.0
|
||||
github.com/ohler55/ojg v1.12.9
|
||||
github.com/pkg/sftp v1.13.4
|
||||
github.com/robertkrimen/otto v0.0.0-20211024170158-b87d35c0b86f
|
||||
github.com/satori/go.uuid v1.2.0
|
||||
github.com/valyala/fasthttp v1.42.0
|
||||
golang.org/x/crypto v0.1.0
|
||||
golang.org/x/net v0.1.0
|
||||
@@ -41,7 +40,6 @@ require (
|
||||
github.com/golang/protobuf v1.5.2 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/google/btree v1.0.1 // indirect
|
||||
github.com/google/uuid v1.3.0 // indirect
|
||||
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
|
||||
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
|
||||
github.com/hashicorp/go-cleanhttp v0.5.1 // indirect
|
||||
|
||||
@@ -11,7 +11,7 @@ import (
|
||||
"github.com/eolinker/eosc/log"
|
||||
|
||||
"github.com/eolinker/apinto/discovery"
|
||||
"github.com/go-basic/uuid"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// NewHTTPCheck 创建HTTPCheck
|
||||
@@ -80,7 +80,7 @@ func (h *HTTPCheck) doCheckLoop() {
|
||||
|
||||
// Agent 生成一个agent
|
||||
func (h *HTTPCheck) Agent() (discovery.IHealthChecker, error) {
|
||||
return NewAgent(uuid.New(), h), nil
|
||||
return NewAgent(uuid.NewString(), h), nil
|
||||
}
|
||||
|
||||
// Reset 重置HTTPCheck的配置
|
||||
|
||||
@@ -13,7 +13,7 @@ import (
|
||||
|
||||
eoscContext "github.com/eolinker/eosc/eocontext"
|
||||
http_service "github.com/eolinker/eosc/eocontext/http-context"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
uuid "github.com/google/uuid"
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
@@ -169,8 +169,7 @@ func (ctx *HttpContext) Request() http_service.IRequestReader {
|
||||
// NewContext 创建Context
|
||||
func NewContext(ctx *fasthttp.RequestCtx, port int) *HttpContext {
|
||||
|
||||
id := uuid.NewV4()
|
||||
requestID := id.String()
|
||||
requestID := uuid.New().String()
|
||||
newCtx := &HttpContext{
|
||||
fastHttpRequestCtx: ctx,
|
||||
requestID: requestID,
|
||||
|
||||
393
test/body.go
393
test/body.go
@@ -1,393 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"encoding/xml"
|
||||
|
||||
http_service "github.com/eolinker/eosc/eocontext/http-context"
|
||||
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
|
||||
"mime"
|
||||
"mime/multipart"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
const defaultMultipartMemory = 32 << 20 // 32 MB
|
||||
|
||||
const (
|
||||
MultipartForm = "multipart/form-data"
|
||||
FormData = "application/x-www-form-urlencoded"
|
||||
TEXT = "text/plain"
|
||||
JSON = "application/json"
|
||||
JavaScript = "application/javascript"
|
||||
AppLicationXML = "application/xml"
|
||||
TextXML = "text/xml"
|
||||
Html = "text/html"
|
||||
)
|
||||
|
||||
//BodyRequestHandler body请求处理器
|
||||
type BodyRequestHandler struct {
|
||||
form url.Values
|
||||
contentType string
|
||||
files map[string]*http_service.FileHeader
|
||||
|
||||
isInit bool
|
||||
isWriteRaw bool
|
||||
|
||||
object interface{}
|
||||
|
||||
raw []byte
|
||||
}
|
||||
|
||||
//func NewBodyRequestHandler(contentType string,raw) *BodyRequestHandler {
|
||||
// return &BodyRequestHandler{ contentType: contentType,raw: raw}
|
||||
//}
|
||||
|
||||
//GetForm 获取表单参数
|
||||
func (b *BodyRequestHandler) GetForm(key string) string {
|
||||
|
||||
contentType, _, _ := mime.ParseMediaType(b.contentType)
|
||||
if contentType != FormData && contentType != MultipartForm {
|
||||
return ""
|
||||
}
|
||||
b.parse()
|
||||
|
||||
if !b.isInit || b.form == nil {
|
||||
return ""
|
||||
}
|
||||
return b.form.Get(key)
|
||||
}
|
||||
|
||||
//ContentType 获取contentType
|
||||
func (b *BodyRequestHandler) ContentType() string {
|
||||
return b.contentType
|
||||
}
|
||||
|
||||
//BodyForm 获取表单参数
|
||||
func (b *BodyRequestHandler) BodyForm() (url.Values, error) {
|
||||
|
||||
err := b.parse()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b.form, nil
|
||||
}
|
||||
|
||||
//RawBody 获取raw数据
|
||||
func (b *BodyRequestHandler) RawBody() ([]byte, error) {
|
||||
|
||||
err := b.encode()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b.rawBody(), nil
|
||||
|
||||
}
|
||||
|
||||
//Files 获取文件参数
|
||||
func (b *BodyRequestHandler) Files() (map[string]*http_service.FileHeader, error) {
|
||||
|
||||
err := b.parse()
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b.files, nil
|
||||
}
|
||||
|
||||
func (b *BodyRequestHandler) GetFile(key string) (file *http_service.FileHeader, has bool) {
|
||||
err := b.parse()
|
||||
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
file, has = b.files[key]
|
||||
return file, has
|
||||
}
|
||||
|
||||
//parse 解析
|
||||
func (b *BodyRequestHandler) parse() error {
|
||||
if b.isInit {
|
||||
return nil
|
||||
}
|
||||
contentType, _, _ := mime.ParseMediaType(b.contentType)
|
||||
switch contentType {
|
||||
case JSON:
|
||||
{
|
||||
e := json.Unmarshal(b.rawBody(), &b.object)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
}
|
||||
case AppLicationXML, TextXML:
|
||||
{
|
||||
e := xml.Unmarshal(b.rawBody(), &b.object)
|
||||
if e != nil {
|
||||
return e
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
case MultipartForm:
|
||||
{
|
||||
r, err := multipartReader(b.contentType, false, b.rawBody())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
form, err := r.ReadForm(defaultMultipartMemory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if b.form == nil {
|
||||
b.form = make(url.Values)
|
||||
}
|
||||
for k, v := range form.Value {
|
||||
b.form[k] = append(b.form[k], v...)
|
||||
}
|
||||
|
||||
b.files = make(map[string]*http_service.FileHeader)
|
||||
for k, fs := range form.File {
|
||||
|
||||
if len(fs) > 0 {
|
||||
file, err := fs[0].Open()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fileData, err := ioutil.ReadAll(file)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
b.files[k] = &http_service.FileHeader{
|
||||
FileName: fs[0].Filename,
|
||||
Data: fileData,
|
||||
Header: fs[0].Header,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
b.object = b.form
|
||||
}
|
||||
case FormData:
|
||||
{
|
||||
form, err := url.ParseQuery(string(b.rawBody()))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if b.form == nil {
|
||||
b.form = form
|
||||
} else {
|
||||
for k, v := range form {
|
||||
b.form[k] = append(b.form[k], v...)
|
||||
}
|
||||
|
||||
}
|
||||
b.object = b.form
|
||||
|
||||
}
|
||||
}
|
||||
b.isInit = true
|
||||
return nil
|
||||
}
|
||||
|
||||
//SetToForm 设置表单参数
|
||||
func (b *BodyRequestHandler) SetToForm(key, value string) error {
|
||||
|
||||
contentType, _, _ := mime.ParseMediaType(b.contentType)
|
||||
if contentType != FormData && contentType != MultipartForm {
|
||||
return ErrorNotForm
|
||||
}
|
||||
|
||||
err := b.parse()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.isWriteRaw = false
|
||||
|
||||
if b.form == nil {
|
||||
b.form = make(url.Values)
|
||||
}
|
||||
b.form.Set(key, value)
|
||||
b.isWriteRaw = false
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//AddForm 新增表单参数
|
||||
func (b *BodyRequestHandler) AddForm(key, value string) error {
|
||||
contentType, _, _ := mime.ParseMediaType(b.contentType)
|
||||
if contentType != FormData && contentType != MultipartForm {
|
||||
return ErrorNotForm
|
||||
}
|
||||
err := b.parse()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.isWriteRaw = false
|
||||
|
||||
if b.form == nil {
|
||||
b.form = make(url.Values)
|
||||
}
|
||||
b.form.Add(key, value)
|
||||
return nil
|
||||
}
|
||||
|
||||
//AddFile 新增文件参数
|
||||
func (b *BodyRequestHandler) AddFile(key string, file *http_service.FileHeader) error {
|
||||
|
||||
contentType, _, _ := mime.ParseMediaType(b.contentType)
|
||||
if contentType != FormData && contentType != MultipartForm {
|
||||
return ErrorNotMultipart
|
||||
}
|
||||
err := b.parse()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.isWriteRaw = false
|
||||
if file == nil && b.files != nil {
|
||||
delete(b.files, key)
|
||||
return nil
|
||||
}
|
||||
if b.files == nil {
|
||||
b.files = make(map[string]*http_service.FileHeader)
|
||||
}
|
||||
b.files[key] = file
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
////Clone 克隆body
|
||||
//func (b *BodyRequestHandler) Clone() *BodyRequestHandler {
|
||||
// rawBody, _ := b.RawBody()
|
||||
// return newBodyRequestHandler(b.contentType, rawBody)
|
||||
//
|
||||
//}
|
||||
|
||||
//BodyInterface 获取请求体对象
|
||||
func (b *BodyRequestHandler) BodyInterface() (interface{}, error) {
|
||||
err := b.parse()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return b.object, nil
|
||||
}
|
||||
|
||||
//encode encode
|
||||
func (b *BodyRequestHandler) encode() error {
|
||||
if b.isWriteRaw {
|
||||
return nil
|
||||
}
|
||||
|
||||
contentType, _, _ := mime.ParseMediaType(b.contentType)
|
||||
if contentType != FormData && contentType != MultipartForm {
|
||||
b.isWriteRaw = true
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(b.files) > 0 {
|
||||
body := new(bytes.Buffer)
|
||||
writer := multipart.NewWriter(body)
|
||||
|
||||
for name, file := range b.files {
|
||||
part, err := writer.CreateFormFile(name, file.FileName)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = part.Write(file.Data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
for key, values := range b.form {
|
||||
temp := make(url.Values)
|
||||
temp[key] = values
|
||||
value := temp.Encode()
|
||||
err := writer.WriteField(key, value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
err := writer.Close()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b.contentType = writer.FormDataContentType()
|
||||
b.isWriteRaw = true
|
||||
} else {
|
||||
if b.form != nil {
|
||||
b.raw = []byte(b.form.Encode())
|
||||
} else {
|
||||
b.raw = nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
//SetForm 设置表单参数
|
||||
func (b *BodyRequestHandler) SetForm(values url.Values) error {
|
||||
|
||||
contentType, _, _ := mime.ParseMediaType(b.contentType)
|
||||
if contentType != FormData && contentType != MultipartForm {
|
||||
return ErrorNotForm
|
||||
}
|
||||
b.parse()
|
||||
b.form = values
|
||||
b.isWriteRaw = false
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//SetFile 设置文件参数
|
||||
func (b *BodyRequestHandler) SetFile(files map[string]*http_service.FileHeader) error {
|
||||
|
||||
contentType, _, _ := mime.ParseMediaType(b.contentType)
|
||||
if contentType != FormData && contentType != MultipartForm {
|
||||
return ErrorNotForm
|
||||
}
|
||||
b.parse()
|
||||
b.files = files
|
||||
// b.form = values
|
||||
b.isWriteRaw = false
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//SetRaw 设置raw数据
|
||||
func (b *BodyRequestHandler) SetRaw(contentType string, body []byte) {
|
||||
b.contentType, b.isInit, b.isWriteRaw = contentType, false, true
|
||||
return
|
||||
|
||||
}
|
||||
func (b *BodyRequestHandler) rawBody() []byte {
|
||||
return b.raw
|
||||
}
|
||||
|
||||
////newBodyRequestHandler 创建body请求处理器
|
||||
//func newBodyRequestHandler(contentType string, body []byte) *BodyRequestHandler {
|
||||
// b := new(BodyRequestHandler)
|
||||
// b.SetRaw(contentType, body)
|
||||
// return b
|
||||
//}
|
||||
|
||||
func multipartReader(contentType string, allowMixed bool, raw []byte) (*multipart.Reader, error) {
|
||||
|
||||
if contentType == "" {
|
||||
return nil, http.ErrNotMultipart
|
||||
}
|
||||
d, params, err := mime.ParseMediaType(contentType)
|
||||
if err != nil || !(d == "multipart/form-data" || allowMixed && d == "multipart/mixed") {
|
||||
return nil, http.ErrNotMultipart
|
||||
}
|
||||
boundary, ok := params["boundary"]
|
||||
if !ok {
|
||||
return nil, http.ErrMissingBoundary
|
||||
}
|
||||
body := ioutil.NopCloser(bytes.NewBuffer(raw))
|
||||
return multipart.NewReader(body, boundary), nil
|
||||
}
|
||||
@@ -1,75 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
http_service "github.com/eolinker/eosc/eocontext/http-context"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrorNotForm = errors.New("contentType is not Form")
|
||||
ErrorNotMultipart = errors.New("contentType is not Multipart")
|
||||
ErrorNotAllowRaw = errors.New("contentType is not allow Raw")
|
||||
ErrorNotSend = errors.New("not send")
|
||||
)
|
||||
var _ http_service.IHttpContext = (*Context)(nil)
|
||||
|
||||
type ResponseDO interface {
|
||||
Response(request *RequestReader) (*Response, error)
|
||||
}
|
||||
type Context struct {
|
||||
proxyRequest *ProxyRequest
|
||||
requestID string
|
||||
response *Response
|
||||
responseError error
|
||||
requestReader *RequestReader
|
||||
ctx context.Context
|
||||
|
||||
responseHandler ResponseDO
|
||||
}
|
||||
|
||||
func (c *Context) RequestId() string {
|
||||
return c.requestID
|
||||
}
|
||||
|
||||
func (c *Context) Context() context.Context {
|
||||
if c.ctx == nil {
|
||||
c.ctx = context.Background()
|
||||
}
|
||||
return c.ctx
|
||||
}
|
||||
|
||||
func (c *Context) Value(key interface{}) interface{} {
|
||||
return c.Context().Value(key)
|
||||
}
|
||||
|
||||
func (c *Context) WithValue(key, val interface{}) {
|
||||
c.ctx = context.WithValue(c.Context(), key, val)
|
||||
}
|
||||
|
||||
func (c *Context) Request() http_service.IRequestReader {
|
||||
return c.requestReader
|
||||
}
|
||||
|
||||
func (c *Context) Proxy() http_service.IRequest {
|
||||
return c.proxyRequest
|
||||
}
|
||||
|
||||
func (c *Context) Response() http_service.IResponse {
|
||||
return c.response
|
||||
}
|
||||
|
||||
func (c *Context) ResponseError() error {
|
||||
return c.responseError
|
||||
}
|
||||
|
||||
func (c *Context) SendTo(address string, timeout time.Duration) error {
|
||||
|
||||
if c.responseHandler != nil {
|
||||
c.response, c.responseError = c.responseHandler.Response(c.proxyRequest.RequestReader)
|
||||
}
|
||||
|
||||
return c.responseError
|
||||
}
|
||||
@@ -1,79 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type RequestHeader struct {
|
||||
host string
|
||||
method string
|
||||
header http.Header
|
||||
}
|
||||
|
||||
func (r *RequestHeader) SetHeader(key, value string) {
|
||||
|
||||
r.header.Set(key, value)
|
||||
}
|
||||
|
||||
func (r *RequestHeader) AddHeader(key, value string) {
|
||||
r.header.Add(key, value)
|
||||
}
|
||||
|
||||
func (r *RequestHeader) DelHeader(key string) {
|
||||
r.header.Del(key)
|
||||
}
|
||||
|
||||
func (r *RequestHeader) SetHost(host string) {
|
||||
r.host = host
|
||||
}
|
||||
|
||||
func (r *RequestHeader) GetHeader(name string) string {
|
||||
return r.header.Get(name)
|
||||
}
|
||||
|
||||
func (r *RequestHeader) Headers() http.Header {
|
||||
return r.header
|
||||
}
|
||||
|
||||
func (r *RequestHeader) Host() string {
|
||||
return r.host
|
||||
}
|
||||
|
||||
func (r *RequestHeader) Method() string {
|
||||
return r.method
|
||||
}
|
||||
func (r *RequestHeader) SetMethod(method string) {
|
||||
r.method = method
|
||||
}
|
||||
|
||||
type ResponseHeader struct {
|
||||
header http.Header
|
||||
}
|
||||
|
||||
func NewResponseHeader() *ResponseHeader {
|
||||
return &ResponseHeader{header: make(http.Header)}
|
||||
}
|
||||
|
||||
func (r *ResponseHeader) GetHeader(name string) string {
|
||||
return r.header.Get(name)
|
||||
}
|
||||
|
||||
func (r *ResponseHeader) Headers() http.Header {
|
||||
|
||||
return r.header
|
||||
}
|
||||
|
||||
func (r *ResponseHeader) SetHeader(key, value string) {
|
||||
|
||||
r.header.Set(key, value)
|
||||
}
|
||||
|
||||
func (r *ResponseHeader) AddHeader(key, value string) {
|
||||
|
||||
r.header.Add(key, value)
|
||||
}
|
||||
|
||||
func (r *ResponseHeader) DelHeader(key string) {
|
||||
|
||||
r.header.Del(key)
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
http_service "github.com/eolinker/eosc/eocontext/http-context"
|
||||
)
|
||||
|
||||
var _ http_service.IRequest = (*ProxyRequest)(nil)
|
||||
|
||||
type ProxyRequest struct {
|
||||
*RequestReader
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) Finish() error {
|
||||
return nil
|
||||
}
|
||||
func (r *ProxyRequest) Header() http_service.IHeaderWriter {
|
||||
return r.headers
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) Body() http_service.IBodyDataWriter {
|
||||
return r.body
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) URI() http_service.IURIWriter {
|
||||
return r.uri
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) SetPath(s string) {
|
||||
r.URI().SetPath(s)
|
||||
}
|
||||
|
||||
func NewProxyRequest(remoteAddr string) *ProxyRequest {
|
||||
|
||||
return &ProxyRequest{
|
||||
RequestReader: &RequestReader{
|
||||
|
||||
remoteAddr: remoteAddr,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *ProxyRequest) SetMethod(s string) {
|
||||
r.headers.SetMethod(s)
|
||||
}
|
||||
@@ -1,41 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
http_service "github.com/eolinker/eosc/eocontext/http-context"
|
||||
)
|
||||
|
||||
type RequestReader struct {
|
||||
headers *RequestHeader
|
||||
body *BodyRequestHandler
|
||||
uri *URIRequest
|
||||
remoteAddr string
|
||||
method string
|
||||
}
|
||||
|
||||
func (r *RequestReader) Header() http_service.IHeaderReader {
|
||||
return r.headers
|
||||
}
|
||||
|
||||
func (r *RequestReader) Body() http_service.IBodyDataReader {
|
||||
return r.body
|
||||
}
|
||||
|
||||
func (r *RequestReader) RemoteAddr() string {
|
||||
return r.remoteAddr
|
||||
}
|
||||
|
||||
func (r *RequestReader) ReadIP() string {
|
||||
return r.remoteAddr
|
||||
}
|
||||
|
||||
func (r *RequestReader) ForwardIP() string {
|
||||
return r.ForwardIP()
|
||||
}
|
||||
|
||||
func (r *RequestReader) URI() http_service.IURIReader {
|
||||
return r.uri
|
||||
}
|
||||
|
||||
func (r *RequestReader) Method() string {
|
||||
return r.method
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
http_service "github.com/eolinker/eosc/eocontext/http-context"
|
||||
)
|
||||
|
||||
var _ http_service.IResponse = (*Response)(nil)
|
||||
|
||||
type Response struct {
|
||||
*ResponseHeader
|
||||
body []byte
|
||||
statusCode int
|
||||
}
|
||||
|
||||
func (r *Response) reset() error {
|
||||
r.ResponseHeader.header = make(http.Header)
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewResponse() *Response {
|
||||
return &Response{ResponseHeader: NewResponseHeader()}
|
||||
}
|
||||
|
||||
func (r *Response) GetBody() []byte {
|
||||
return r.body
|
||||
}
|
||||
|
||||
func (r *Response) StatusCode() int {
|
||||
return r.statusCode
|
||||
}
|
||||
|
||||
func (r *Response) Status() string {
|
||||
return strconv.Itoa(r.statusCode)
|
||||
|
||||
}
|
||||
|
||||
func (r *Response) SetStatus(code int, status string) {
|
||||
r.statusCode = code
|
||||
}
|
||||
|
||||
func (r *Response) SetBody(bytes []byte) {
|
||||
r.body = bytes
|
||||
}
|
||||
143
test/test.go
143
test/test.go
@@ -1,143 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
|
||||
http_service "github.com/eolinker/eosc/eocontext/http-context"
|
||||
uuid "github.com/satori/go.uuid"
|
||||
)
|
||||
|
||||
type IConfig interface {
|
||||
Create() http_service.IHttpContext
|
||||
}
|
||||
type RequestConfig struct {
|
||||
ContentType string
|
||||
Body []byte
|
||||
Header http.Header
|
||||
}
|
||||
type ResponseConfig struct {
|
||||
Body []byte
|
||||
ContendType string
|
||||
Header http.Header
|
||||
StatusCode int
|
||||
ResponseError error
|
||||
}
|
||||
|
||||
func (r *ResponseConfig) Response(request *RequestReader) (*Response, error) {
|
||||
|
||||
header := r.Header
|
||||
if header == nil {
|
||||
header = make(http.Header)
|
||||
}
|
||||
header.Set("content-type", r.ContendType)
|
||||
header.Set("content-length", strconv.Itoa(len(r.Body)))
|
||||
|
||||
return &Response{
|
||||
ResponseHeader: &ResponseHeader{header: r.Header},
|
||||
body: r.Body,
|
||||
statusCode: r.StatusCode,
|
||||
}, nil
|
||||
}
|
||||
|
||||
type Config struct {
|
||||
Method string
|
||||
Url string
|
||||
|
||||
Request *RequestConfig
|
||||
|
||||
Response ResponseDO
|
||||
}
|
||||
|
||||
func (c *Config) Create() http_service.IHttpContext {
|
||||
requestUri, err := url.Parse(c.Url)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if c.Request == nil {
|
||||
c.Request = &RequestConfig{
|
||||
ContentType: "",
|
||||
Body: nil,
|
||||
Header: make(http.Header),
|
||||
}
|
||||
} else {
|
||||
if c.Request.Header == nil {
|
||||
c.Request.Header = make(http.Header)
|
||||
}
|
||||
}
|
||||
c.Request.Header.Set("contend-type", c.Request.ContentType)
|
||||
c.Request.Header.Set("content-length", strconv.Itoa(len(c.Request.Body)))
|
||||
return &Context{
|
||||
|
||||
proxyRequest: &ProxyRequest{},
|
||||
requestID: uuid.NewV4().String(),
|
||||
response: NewResponse(),
|
||||
responseHandler: c.Response,
|
||||
responseError: nil,
|
||||
requestReader: &RequestReader{
|
||||
headers: &RequestHeader{
|
||||
header: c.Request.Header,
|
||||
},
|
||||
body: &BodyRequestHandler{
|
||||
contentType: c.Request.ContentType,
|
||||
raw: c.Request.Body,
|
||||
},
|
||||
uri: &URIRequest{
|
||||
url: requestUri,
|
||||
},
|
||||
remoteAddr: "127.0.0.1",
|
||||
method: c.Method,
|
||||
},
|
||||
ctx: nil,
|
||||
}
|
||||
}
|
||||
func NewGet(url string, response ResponseDO) http_service.IHttpContext {
|
||||
c := &Config{
|
||||
Method: http.MethodGet,
|
||||
Url: url,
|
||||
Request: nil,
|
||||
Response: response,
|
||||
}
|
||||
return c.Create()
|
||||
}
|
||||
func NewPostJson(url string, body interface{}, response ResponseDO) http_service.IHttpContext {
|
||||
bodyData, _ := json.Marshal(body)
|
||||
|
||||
c := &Config{
|
||||
Method: http.MethodPost,
|
||||
Url: url,
|
||||
Request: &RequestConfig{
|
||||
ContentType: "application/json",
|
||||
Body: bodyData,
|
||||
Header: make(http.Header),
|
||||
},
|
||||
Response: response,
|
||||
}
|
||||
return c.Create()
|
||||
|
||||
}
|
||||
|
||||
func JsonResponse(body interface{}) *ResponseConfig {
|
||||
bodyData, _ := json.Marshal(body)
|
||||
return &ResponseConfig{
|
||||
Body: bodyData,
|
||||
ContendType: "application/json",
|
||||
Header: make(http.Header),
|
||||
StatusCode: 200,
|
||||
ResponseError: nil,
|
||||
}
|
||||
}
|
||||
|
||||
type PrintResponse struct {
|
||||
}
|
||||
|
||||
func (p *PrintResponse) Response(request *RequestReader) (*Response, error) {
|
||||
body := make(map[string]interface{})
|
||||
body["url"] = request.uri.RequestURI()
|
||||
body["host"] = request.uri.Host()
|
||||
body["body"], _ = request.Body().RawBody()
|
||||
|
||||
return JsonResponse(body).Response(request)
|
||||
}
|
||||
75
test/uri.go
75
test/uri.go
@@ -1,75 +0,0 @@
|
||||
package test
|
||||
|
||||
import (
|
||||
"net/url"
|
||||
|
||||
http_service "github.com/eolinker/eosc/eocontext/http-context"
|
||||
)
|
||||
|
||||
var _ http_service.IURIWriter = (*URIRequest)(nil)
|
||||
|
||||
type URIRequest struct {
|
||||
url *url.URL
|
||||
}
|
||||
|
||||
func (u *URIRequest) RequestURI() string {
|
||||
return u.url.RequestURI()
|
||||
}
|
||||
|
||||
func (u *URIRequest) Scheme() string {
|
||||
return u.url.Scheme
|
||||
}
|
||||
|
||||
func (u *URIRequest) RawURL() string {
|
||||
return u.RawURL()
|
||||
}
|
||||
|
||||
func (u *URIRequest) Host() string {
|
||||
return u.url.Host
|
||||
}
|
||||
|
||||
func (u *URIRequest) Path() string {
|
||||
return u.url.Path
|
||||
}
|
||||
|
||||
func (u *URIRequest) GetQuery(key string) string {
|
||||
return u.url.Query().Get(key)
|
||||
}
|
||||
|
||||
func (u *URIRequest) RawQuery() string {
|
||||
return u.url.RawQuery
|
||||
}
|
||||
|
||||
func (u *URIRequest) SetQuery(key, value string) {
|
||||
query := u.url.Query()
|
||||
query.Set(key, value)
|
||||
u.url.RawQuery = query.Encode()
|
||||
}
|
||||
|
||||
func (u *URIRequest) AddQuery(key, value string) {
|
||||
query := u.url.Query()
|
||||
query.Add(key, value)
|
||||
u.url.RawQuery = query.Encode()
|
||||
}
|
||||
|
||||
func (u *URIRequest) DelQuery(key string) {
|
||||
query := u.url.Query()
|
||||
query.Del(key)
|
||||
u.url.RawQuery = query.Encode()
|
||||
}
|
||||
|
||||
func (u *URIRequest) SetRawQuery(raw string) {
|
||||
u.url.RawQuery = raw
|
||||
}
|
||||
|
||||
func (u *URIRequest) SetPath(s string) {
|
||||
u.url.Path = s
|
||||
}
|
||||
|
||||
func (u *URIRequest) SetScheme(scheme string) {
|
||||
u.url.Scheme = scheme
|
||||
}
|
||||
|
||||
func (u *URIRequest) SetHost(host string) {
|
||||
u.url.Host = host
|
||||
}
|
||||
Reference in New Issue
Block a user