Skip to content
Myr

gin.Context with fallback value from gin.Context.Request.Context() (#2751)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* Update tree.go (#2659) delete more "()" * updated comments for Get function for params (#2756) * ci: add github action workflows (#2596) * ci: add github action workflows * test: fixed the TestUnixSocket test on windows (#20) * ci: add github action workflows (#18) * Remove .travis.yml * ci: replace GITTER_ROOM_ID and upload coverage every time you go test * ci: update coverage using codecov/codecov-action@v1 * Merge branch 'master' into github-actions * repo: replace travis ci to github actions * ci: add go version 1.16 * fix: go install requires a specific version * chore(ci): remove go 1.12 support * chore(ci): remove os windows-latest Co-authored-by: thinkerou <thinkerou@gmail.com> Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com> * Setting trusted platform using an enum-like (#2739) * gin.Context with fallback value from c.Request.Context() * add test case

authored by wei · committed by GitHub · parent 09f6cff · Browse files · 2 files+60 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: youzeliang <youzel@126.com>
  • Co-authored-by: Ashwani <ashwanisharma686@gmail.com>
  • Co-authored-by: Jeff <laojianzi1994@gmail.com>
  • Co-authored-by: thinkerou <thinkerou@gmail.com>
  • Co-authored-by: Bo-Yi Wu <appleboy.tw@gmail.com>
  • Co-authored-by: Alessandro (Ale) Segala <43508+ItalyPaleAle@users.noreply.github.com>

Diff stats

2 files+60 added−3 removed

  • other:+60 added−3 removed

2 files+60 added−3 removed

modified:context_test.go+53 added
2055 unmodified lines
2056 ip, trust := c.RemoteIP()2057 assert.Nil(t, ip)2058 assert.False(t, trust)2059}20602061func TestContextWithFallbackValueFromRequestContext(t *testing.T) {2062 tests := []struct {2063 name string2064 getContextAndKey func() (*Context, interface{})2065 value interface{}2066 }{2067 {2068 name: "c with struct context key",2069 getContextAndKey: func() (*Context, interface{}) {2070 var key struct{}2071 c := &Context{}2072 c.Request, _ = http.NewRequest("POST", "/", nil)2073 c.Request = c.Request.WithContext(context.WithValue(context.TODO(), key, "value"))2074 return c, key2075 },2076 value: "value",2077 },2078 {2079 name: "c with string context key",2080 getContextAndKey: func() (*Context, interface{}) {2081 c := &Context{}2082 c.Request, _ = http.NewRequest("POST", "/", nil)2083 c.Request = c.Request.WithContext(context.WithValue(context.TODO(), "key", "value"))2084 return c, "key"2085 },2086 value: "value",2087 },2088 {2089 name: "c with nil http.Request",2090 getContextAndKey: func() (*Context, interface{}) {2091 c := &Context{}2092 return c, "key"2093 },2094 value: nil,2095 },2096 {2097 name: "c with nil http.Request.Context()",2098 getContextAndKey: func() (*Context, interface{}) {2099 c := &Context{}2100 c.Request, _ = http.NewRequest("POST", "/", nil)2101 return c, "key"2102 },2103 value: nil,2104 },2105 }2106 for _, tt := range tests {2107 t.Run(tt.name, func(t *testing.T) {2108 c, key := tt.getContextAndKey()2109 assert.Equal(t, tt.value, c.Value(key))2110 })2111 }2112}
modified:context.go+7 added-3 removed
1184 unmodified lines
1185 if key == 0 {1186 return c.Request1187 }1188 if keyAsString, ok := key.(string); ok {1189 val, _ := c.Get(keyAsString)1190 return val1189 if val, exists := c.Get(keyAsString); exists {1190 return val1191 }1192 }1193 if c.Request == nil || c.Request.Context() == nil {1192 return nil1194 return nil1195 }1196 return c.Request.Context().Value(key)1197}