func testGetHlsStreamingSessionUrl()

in AWSKinesisVideoArchivedMediaTests/AWSKinesisVideoArchivedMediaTests.swift [66:160]


    func testGetHlsStreamingSessionUrl() {
        let expectation = self.expectation(description: "Got the endpoint")
        let streamName = streamPrefix + String(Date().timeIntervalSince1970)
        var clientDataEndpoint: String?
        
        let kvClient = AWSKinesisVideo.default()
        let createStreamRequest = AWSKinesisVideoCreateStreamInput()
        createStreamRequest?.streamName = streamName
        createStreamRequest?.dataRetentionInHours = 2
        createStreamRequest?.deviceName = "integration-test"
        createStreamRequest?.mediaType = "video/h264"
        
        kvClient.createStream(createStreamRequest!, completionHandler: {(createResult, error) -> Void in
            if let error = error {
                XCTAssertNil(error)
                return
            }

            guard let _ = createResult else {
                XCTFail("Failed to create stream.")
                return
            }
            
            let getDataEndpointRequest = AWSKinesisVideoGetDataEndpointInput()
            getDataEndpointRequest?.streamName = streamName
            getDataEndpointRequest?.apiName = AWSKinesisVideoAPIName.getHlsStreamingSessionUrl
            kvClient.getDataEndpoint(getDataEndpointRequest!, completionHandler: { (dataEndpointResult, error) in
                if let error = error {
                    XCTAssertNil(error)
                    return
                }

                guard let dataEndpointResult = dataEndpointResult else {
                    XCTFail("Failed to get data endpoint.")
                    return
                }
                
                guard let endpoint = dataEndpointResult.dataEndpoint else {
                    XCTFail("Data endpoint is null")
                    return
                }
                
                let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
                let endpointLength = endpoint.count
                let range = NSRange(location: 0, length: endpointLength)
                guard let match = detector.firstMatch(in: endpoint, options: [], range: range) else {
                    XCTFail("Data endpoint is malformed")
                    return
                }
                
                XCTAssertTrue(match.range.length == endpointLength, "The data endpoint was not the only thing in the response, possible malformed URL")
                clientDataEndpoint = endpoint
                expectation.fulfill()
            })
        })
        wait(for: [expectation], timeout: 10)
        
        let streamExpectation = self.expectation(description: "Got the stream")
        let region = AWSTestUtility.getRegionFromTestConfiguration()
        let awsEndpoint = AWSEndpoint(region: region, service: .KinesisVideoArchivedMedia, url: URL(string: clientDataEndpoint!))
        let serviceConfig = AWSServiceConfiguration(region: region, endpoint: awsEndpoint, credentialsProvider: AWSServiceManager.default().defaultServiceConfiguration.credentialsProvider)
        let kvamClientKey = "testGetHlsStreamingSessionUrl"
        AWSKinesisVideoArchivedMedia.register(with: serviceConfig!, forKey: kvamClientKey)
        let kvamClient = AWSKinesisVideoArchivedMedia(forKey: kvamClientKey)
        
        let streamRequest = AWSKinesisVideoArchivedMediaGetHLSStreamingSessionURLInput()
        streamRequest?.streamName = streamName
        streamRequest?.playbackMode = .live
        kvamClient.getHLSStreamingSessionURL(streamRequest!) { (result, error) in
            guard let result = result,
                let endpoint = result.hlsStreamingSessionURL else {
                    let nsError = error! as NSError
                    // Nothing will be streaming during the test
                    // This error differentiates between another that says stream does not exist
                    // This error code does not sufficiently check that the stream has "No fragments found in the stream for the HLS streaming request"
                    // But it is all that is available in iOS
                    // TODO Stream data in parallel
                    XCTAssertTrue(nsError.code == AWSKinesisVideoArchivedMediaErrorType.resourceNotFound.rawValue)
                    streamExpectation.fulfill()
                    return
            }
            
            let detector = try! NSDataDetector(types: NSTextCheckingResult.CheckingType.link.rawValue)
            let endpointLength = endpoint.count
            let range = NSRange(location: 0, length: endpointLength)
            guard let match = detector.firstMatch(in: endpoint, options: [], range: range) else {
                XCTFail("Streaming url is malformed")
                return
            }
            
            XCTAssertTrue(match.range.length == endpointLength, "The streaming url was not the only thing in the response, possible malformed URL")
            streamExpectation.fulfill()
        }
        wait(for: [streamExpectation], timeout: 10)
    }