     XCTAssertEqual(location["weather"]["temperature"].intValue, 70)
   }
   
+  func getAccessToken(completionHandler: @escaping (_ accessToken: String) -> Void) {
+    URLRequest(forTestWithMethod: "GET", route: "token")
+      .sendForTesting { data in
+        let tokenData = JSON(data: data)
+        let accessToken = tokenData["access_token"].stringValue
+        completionHandler(accessToken)
+    }
+  }
+  
 }
 
 private extension URLRequest {

    
    let imageExpectation = expectation(description: "Post an image with server.")
    
    URLRequest(forTestWithMethod: "GET", route: "token")
      .sendForTesting { data in
        
        let tokenData = JSON(data: data)
        let accessToken = tokenData["access_token"].stringValue
    
        // find image
        let fileName = "city.png"
        let initialPath = #file
        let components = initialPath.characters.split(separator: "/").map(String.init)
        let notLastFour = components[0..<components.count - 4]
        let directoryPath = "/" + notLastFour.joined(separator: "/") + "/Cloud-Scripts/Object-Storage/images/city.png"
        let imageURL = URL(fileURLWithPath: directoryPath)
        
        let imageDictionary = ["fileName": fileName, "caption" : "my caption", "width" : 250, "height" : 300, "location" : ["name" : "Austin, TX", "latitude" : 34.53, "longitude" : 84.5]] as [String : Any]
        let boundary = "Boundary-\(UUID().uuidString)"
        let mimeType = "image/png"
        
        do {
          let imageData = try Data(contentsOf: imageURL)
          let jsonData = try JSONSerialization.data(withJSONObject: imageDictionary, options: JSONSerialization.WritingOptions(rawValue: 0))
          let tempJsonString = String(data: jsonData, encoding: String.Encoding.utf8)
          var request = URLRequest(url: URL(string: "http://127.0.0.1:8090/images")!, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 10.0)
          request.timeoutInterval = 60
          request.httpMethod = "POST"
          request.setValue("multipart/form-data; boundary=\(boundary)", forHTTPHeaderField: "Content-Type")
          request.setValue("Bearer \(accessToken)", forHTTPHeaderField: "Authorization")
          
          var body = Data()
          guard let jsonString = tempJsonString, let boundaryStart = "--\(boundary)\r\n".data(using: String.Encoding.utf8),
            let dispositionEncoding = "Content-Disposition:form-data; name=\"imageJson\"\r\n\r\n".data(using: String.Encoding.utf8),
            let jsonEncoding = "\(jsonString)\r\n".data(using: String.Encoding.utf8),
            let imageDispositionEncoding = "Content-Disposition:form-data; name=\"imageBinary\"; filename=\"\(fileName)\"\r\n".data(using: String.Encoding.utf8),
            let imageTypeEncoding = "Content-Type: \(mimeType)\r\n\r\n".data(using: String.Encoding.utf8),
            let imageEndEncoding = "\r\n".data(using: String.Encoding.utf8),
            let boundaryEnd = "--\(boundary)--\r\n".data(using: String.Encoding.utf8) else {
              XCTFail("Post New Image Error: Could not encode all values for multipart data")
              return
          }
          body.append(boundaryStart)
          body.append(dispositionEncoding)
          body.append(jsonEncoding)
          body.append(boundaryStart)
          body.append(imageDispositionEncoding)
          body.append(imageTypeEncoding)
          body.append(imageData)
          body.append(imageEndEncoding)
          body.append(boundaryEnd)
          request.httpBody = body

          print("executing request: \(request)")
          URLSession(configuration: .default).dataTask(with: request) { data, response, error in
            if let error = error {
              XCTFail("Image Post failed with error: \(error)")
            } else {
              print("Image saved")
              imageExpectation.fulfill()
            }
          }
          
          
        } catch {
          XCTFail("Failed to convert image dictionary to binary data.")
        }
    }

    waitForExpectations(timeout: 18.0, handler: nil)
  }

}

    
    let pingExpectation = expectation(description: "Hit ping endpoint and get simple text response.")
    
    self.getAccessToken { accessToken in
      URLRequest(forTestWithMethod: "GET", route: "ping", authToken: accessToken)
        .sendForTesting { data in
              
          let pingResult = String(data: data, encoding: String.Encoding.utf8)!
          XCTAssertTrue(pingResult.contains("Hello World"))
          pingExpectation.fulfill()
      }
    }
    waitForExpectations(timeout: 10.0, handler: nil)
  }
