github.com/bazelbuild/rules_webtesting@v0.2.0/go/wtl/proxy/driverhub/driver_responses.go (about)

     1  // Copyright 2016 Google Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package driverhub
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"log"
    21  	"net/http"
    22  
    23  	"github.com/bazelbuild/rules_webtesting/go/httphelper"
    24  )
    25  
    26  const contentType = "application/json; charset=utf-8"
    27  
    28  func success(w http.ResponseWriter, value interface{}) {
    29  	body, err := json.Marshal(map[string]interface{}{
    30  		"status": 0,
    31  		"value":  value,
    32  	})
    33  	if err != nil {
    34  		log.Printf("Error marshalling json: %v", err)
    35  	}
    36  	w.Header().Set("Content-Type", contentType)
    37  	httphelper.SetDefaultResponseHeaders(w.Header())
    38  	w.WriteHeader(http.StatusOK)
    39  	w.Write(body)
    40  }
    41  
    42  func sessionNotCreated(w http.ResponseWriter, err error) {
    43  	body, err := json.Marshal(map[string]interface{}{
    44  		"status":  33,
    45  		"error":   "session not created",
    46  		"message": err.Error(),
    47  		"value": map[string]string{
    48  			"message": err.Error(),
    49  		},
    50  	})
    51  	if err != nil {
    52  		log.Printf("Error marshalling json: %v", err)
    53  	}
    54  	w.Header().Set("Content-Type", contentType)
    55  	httphelper.SetDefaultResponseHeaders(w.Header())
    56  	w.WriteHeader(http.StatusInternalServerError)
    57  	w.Write(body)
    58  }
    59  
    60  func unknownError(w http.ResponseWriter, err error) {
    61  	body, err := json.Marshal(map[string]interface{}{
    62  		"status":  13,
    63  		"error":   "unknown error",
    64  		"message": err.Error(),
    65  		"value": map[string]string{
    66  			"message": err.Error(),
    67  		},
    68  	})
    69  	if err != nil {
    70  		log.Printf("Error marshalling json: %v", err)
    71  	}
    72  	w.Header().Set("Content-Type", contentType)
    73  	httphelper.SetDefaultResponseHeaders(w.Header())
    74  	w.WriteHeader(http.StatusInternalServerError)
    75  	w.Write(body)
    76  }
    77  
    78  func unknownMethod(w http.ResponseWriter, r *http.Request) {
    79  	message := fmt.Sprintf("%s is not a supported method for %s", r.Method, r.URL.Path)
    80  	body, err := json.Marshal(map[string]interface{}{
    81  		"status":  9,
    82  		"error":   "unknown method",
    83  		"message": message,
    84  		"value": map[string]string{
    85  			"message": message,
    86  		},
    87  	})
    88  	if err != nil {
    89  		log.Printf("Error marshalling json: %v", err)
    90  	}
    91  	w.Header().Set("Content-Type", contentType)
    92  	httphelper.SetDefaultResponseHeaders(w.Header())
    93  	w.WriteHeader(http.StatusMethodNotAllowed)
    94  	w.Write(body)
    95  }
    96  
    97  func unknownCommand(w http.ResponseWriter, r *http.Request) {
    98  	message := fmt.Sprintf("%s %s is an unsupported webdriver command", r.Method, r.URL.Path)
    99  	body, err := json.Marshal(map[string]interface{}{
   100  		"status":  9,
   101  		"error":   "unknown command",
   102  		"message": message,
   103  		"value": map[string]string{
   104  			"message": message,
   105  		},
   106  	})
   107  	if err != nil {
   108  		log.Printf("Error marshalling json: %v", err)
   109  	}
   110  	w.Header().Set("Content-Type", contentType)
   111  	httphelper.SetDefaultResponseHeaders(w.Header())
   112  	w.WriteHeader(http.StatusNotFound)
   113  	w.Write(body)
   114  }
   115  
   116  func invalidSessionID(w http.ResponseWriter, id string) {
   117  	message := fmt.Sprintf("session %s does not exist", id)
   118  	body, err := json.Marshal(map[string]interface{}{
   119  		"status":  6,
   120  		"error":   "invalid session id",
   121  		"message": message,
   122  		"value": map[string]string{
   123  			"message": message,
   124  		},
   125  	})
   126  	if err != nil {
   127  		log.Printf("Error marshalling json: %v", err)
   128  	}
   129  	w.Header().Set("Content-Type", contentType)
   130  	httphelper.SetDefaultResponseHeaders(w.Header())
   131  	w.WriteHeader(http.StatusNotFound)
   132  	w.Write(body)
   133  }
   134  
   135  func timeout(w http.ResponseWriter, endpoint string) {
   136  	message := fmt.Sprintf("request to %q timed out", endpoint)
   137  	body, err := json.Marshal(map[string]interface{}{
   138  		"status":  21,
   139  		"error":   "timeout",
   140  		"message": message,
   141  		"value": map[string]string{
   142  			"message": message,
   143  		},
   144  	})
   145  	if err != nil {
   146  		log.Printf("Error marshalling json: %v", err)
   147  	}
   148  	w.Header().Set("Content-Type", contentType)
   149  	httphelper.SetDefaultResponseHeaders(w.Header())
   150  	w.WriteHeader(http.StatusRequestTimeout)
   151  	w.Write(body)
   152  }