github.com/cloudwego/dynamicgo@v0.2.6-0.20240519101509-707f41b6b834/conv/t2j/conv_amd64_test.go (about) 1 //go:build amd64 && go1.16 2 // +build amd64,go1.16 3 4 /** 5 * Copyright 2023 CloudWeGo Authors. 6 * 7 * Licensed under the Apache License, Version 2.0 (the "License"); 8 * you may not use this file except in compliance with the License. 9 * You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, software 14 * distributed under the License is distributed on an "AS IS" BASIS, 15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 * See the License for the specific language governing permissions and 17 * limitations under the License. 18 */ 19 20 package t2j 21 22 import ( 23 "context" 24 "testing" 25 26 "github.com/cloudwego/dynamicgo/conv" 27 "github.com/cloudwego/dynamicgo/http" 28 "github.com/cloudwego/dynamicgo/thrift" 29 "github.com/davecgh/go-spew/spew" 30 "github.com/stretchr/testify/assert" 31 ) 32 33 func TestConvThrift2HTTP(t *testing.T) { 34 desc := thrift.FnResponse(thrift.GetFnDescFromFile("testdata/idl/example3.thrift", "ExampleMethod", thrift.Options{})) 35 expJSON := getExmaple3JSON() 36 cv := NewBinaryConv(conv.Options{ 37 // MapRecurseDepth: conv.DefaultMaxDepth, 38 EnableValueMapping: true, 39 EnableHttpMapping: true, 40 OmitHttpMappingErrors: true, 41 }) 42 in := getExample3Data() 43 ctx := context.Background() 44 resp := http.NewHTTPResponse() 45 ctx = context.WithValue(ctx, conv.CtxKeyHTTPResponse, resp) 46 out, err := cv.Do(ctx, desc, in) 47 if err != nil { 48 t.Fatal(err) 49 } 50 println(expJSON, string(out)) 51 chechHelper(t, expJSON, string(out), testOptions{ 52 JSConv: true, 53 }) 54 // buf, err := io.ReadAll(resp.Response.Body) 55 // require.Nil(t, err) 56 // chechHelper(t, expJSON, string(buf)) 57 assert.Equal(t, "true", resp.Header["Heeader"][0]) 58 if len(resp.Header) != 2 { 59 spew.Dump(resp.Header) 60 } 61 assert.Equal(t, 2, len(resp.Header)) 62 assert.Equal(t, "cookie", resp.Response.Cookies()[0].Name) 63 assert.Equal(t, "-1e-7", resp.Response.Cookies()[0].Value) 64 assert.Equal(t, "inner_string", resp.Response.Cookies()[1].Name) 65 assert.Equal(t, "hello", resp.Response.Cookies()[1].Value) 66 67 ctx = context.Background() 68 resp = http.NewHTTPResponse() 69 ctx = context.WithValue(ctx, conv.CtxKeyHTTPResponse, resp) 70 out, err = cv.Do(ctx, desc, in) 71 if err != nil { 72 t.Fatal(err) 73 } 74 chechHelper(t, expJSON, string(out), testOptions{ 75 JSConv: true, 76 }) 77 // buf, err = io.ReadAll(resp.Response.Body) 78 // require.Nil(t, err) 79 // chechHelper(t, expJSON, string(buf)) 80 assert.Equal(t, "true", resp.Header["Heeader"][0]) 81 assert.Equal(t, 2, len(resp.Header)) 82 assert.Equal(t, "cookie", resp.Response.Cookies()[0].Name) 83 assert.Equal(t, "-1e-7", resp.Response.Cookies()[0].Value) 84 assert.Equal(t, "inner_string", resp.Response.Cookies()[1].Name) 85 assert.Equal(t, "hello", resp.Response.Cookies()[1].Value) 86 }