@@ -12,6 +12,18 @@ import (
1212 "github.com/mobile-next/mobilecli/utils"
1313)
1414
15+ type alwaysMatch struct {
16+ PlatformName string `json:"platformName"`
17+ }
18+
19+ type sessionCapabilities struct {
20+ AlwaysMatch alwaysMatch `json:"alwaysMatch"`
21+ }
22+
23+ type sessionRequest struct {
24+ Capabilities sessionCapabilities `json:"capabilities"`
25+ }
26+
1527func (c * WdaClient ) GetEndpoint (endpoint string ) (map [string ]interface {}, error ) {
1628 url := fmt .Sprintf ("%s/%s" , c .baseURL , endpoint )
1729
@@ -27,6 +39,7 @@ func (c *WdaClient) GetEndpoint(endpoint string) (map[string]interface{}, error)
2739 if err != nil {
2840 return nil , fmt .Errorf ("failed to fetch endpoint %s: %w" , endpoint , err )
2941 }
42+
3043 defer func () { _ = resp .Body .Close () }()
3144
3245 // check HTTP status code
@@ -63,6 +76,7 @@ func (c *WdaClient) PostEndpoint(endpoint string, data interface{}) (map[string]
6376 if err != nil {
6477 return nil , fmt .Errorf ("failed to create request: %w" , err )
6578 }
79+
6680 req .Header .Set ("Content-Type" , "application/json" )
6781
6882 resp , err := c .httpClient .Do (req )
@@ -144,14 +158,15 @@ func (c *WdaClient) WaitForAgent() error {
144158}
145159
146160func (c * WdaClient ) CreateSession () (string , error ) {
147- response , err := c . PostEndpoint ( "session" , map [ string ] interface {} {
148- "capabilities" : map [ string ] interface {} {
149- "alwaysMatch" : map [ string ] interface {} {
150- "platformName" : "iOS" ,
161+ request := sessionRequest {
162+ Capabilities : sessionCapabilities {
163+ AlwaysMatch : alwaysMatch {
164+ PlatformName : "iOS" ,
151165 },
152166 },
153- })
167+ }
154168
169+ response , err := c .PostEndpoint ("session" , request )
155170 if err != nil {
156171 return "" , fmt .Errorf ("failed to create session: %w" , err )
157172 }
@@ -175,6 +190,7 @@ func (c *WdaClient) GetOrCreateSession() (string, error) {
175190 if c .isSessionStillValid (c .sessionId ) {
176191 return c .sessionId , nil
177192 }
193+
178194 // session is invalid, clear it and create a new one
179195 utils .Verbose ("cached session %s is invalid, creating new session" , c .sessionId )
180196 c .sessionId = ""
@@ -195,5 +211,10 @@ func (c *WdaClient) DeleteSession(sessionId string) error {
195211 if err != nil {
196212 return fmt .Errorf ("failed to delete session %s: %w" , sessionId , err )
197213 }
214+
215+ if c .sessionId == sessionId {
216+ c .sessionId = ""
217+ }
218+
198219 return nil
199220}
0 commit comments