<commit_before>/*
 * Copyright (c) 2011-2016 Pivotal Software Inc, All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package reactor.io.netty.http

import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import reactor.core.util.Exceptions
import reactor.io.netty.impl.netty.http.NettyHttpServer
import reactor.io.netty.preprocessor.CodecPreprocessor
import reactor.io.netty.ReactiveNet
import spock.lang.Specification

import java.time.Duration
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

/**
 * @author <NAME>
 */
class HttpSpec extends Specification {

  def "http responds empty"() {
	given: "a simple HttpServer"

	//Listen on localhost using default impl (Netty) and assign a global codec to receive/reply String data
	def server = ReactiveNet.httpServer {
	  it.httpProcessor(CodecPreprocessor.string()).listen(0)
	}

	when: "the server is prepared"

	//prepare post request consumer on /test/* and capture the URL parameter "param"
	server.post('/test/{param}') {
	   req ->

		//log then transform then log received http request content from the request body and the resolved URL parameter "param"
		//the returned stream is bound to the request stream and will auto read/close accordingly

		req.writeWith(Flux.empty())

	}

	then: "the server was started"
	server
	!server.start().get(Duration.ofSeconds(5))

	when: "data is sent with Reactor HTTP support"

	//Prepare a client using default impl (Netty) to connect on http://localhost:port/ and assign global codec to send/receive String data
	def client = ReactiveNet.httpClient {
	  it.httpProcessor(CodecPreprocessor.string()).connect("localhost", server.listenAddress.port)
	}

	//prepare an http post request-reply flow
	def content = client.post('/test/World') { req ->
	  //prepare content-type
	  req.header('Content-Type', 'text/plain')

	  //return a producing stream to send some data along the request
	  req.writeWith(Mono.just("Hello").log('client-send'))

	}.then { replies ->
	  //successful request, listen for the first returned next reply and pass it downstream
	  replies.log('client-received').next()
	}
	.doOnError {
	  //something failed during the request or the reply processing
	  println "Failed requesting server: $it"
	}

	then: "data was not recieved"
	//the produced reply should be there soon
	!content.get(Duration.ofSeconds(5))
  }


	def "http responds to requests from clients"() {
	given: "a simple HttpServer"

	//Listen on localhost using default impl (Netty) and assign a global codec to receive/reply String data
	def server = ReactiveNet.httpServer {
	  it.httpProcessor(CodecPreprocessor.string()).listen(0)
	}

	when: "the server is prepared"

	//prepare post request consumer on /test/* and capture the URL parameter "param"<commit_msg>fix tests<commit_after>/*
 * Copyright (c) 2011-2016 Pivotal Software Inc, All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package reactor.io.netty.http

import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import reactor.core.util.Exceptions
import reactor.io.netty.impl.netty.http.NettyHttpServer
import reactor.io.netty.preprocessor.CodecPreprocessor
import reactor.io.netty.ReactiveNet
import spock.lang.Specification

import java.time.Duration
import java.util.concurrent.CountDownLatch
import java.util.concurrent.TimeUnit

/**
 * @author <NAME>
 */
class HttpSpec extends Specification {

  def "http responds empty"() {
	given: "a simple HttpServer"

	//Listen on localhost using default impl (Netty) and assign a global codec to receive/reply String data
	def server = ReactiveNet.httpServer {
	  it.httpProcessor(CodecPreprocessor.string()).listen(0)
	}

	when: "the server is prepared"

	//prepare post request consumer on /test/* and capture the URL parameter "param"
	server.post('/test/{param}') {
	   req ->

		//log then transform then log received http request content from the request body and the resolved URL parameter "param"
		//the returned stream is bound to the request stream and will auto read/close accordingly

		req.writeWith(Flux.empty())

	}

	then: "the server was started"
	server
	!server.start().get(Duration.ofSeconds(5))

	when: "data is sent with Reactor HTTP support"

	//Prepare a client using default impl (Netty) to connect on http://localhost:port/ and assign global codec to send/receive String data
	def client = ReactiveNet.httpClient {
	  it.httpProcessor(CodecPreprocessor.string()).connect("localhost", server.listenAddress.port)
	}

	//prepare an http post request-reply flow
	def content = client.post('/test/World') { req ->
	  //prepare content-type
	  req.header('Content-Type', 'text/plain')

	  //return a producing stream to send some data along the request
	  req.writeWith(Mono.just("Hello").log('client-send'))

	}.then { replies ->
	  //successful request, listen for the first returned next reply and pass it downstream
	  replies.input().log('client-received').next()
	}
	.doOnError {
	  //something failed during the request or the reply processing
	  println "Failed requesting server: $it"
	}

	then: "data was not recieved"
	//the produced reply should be there soon
	!content.get(Duration.ofSeconds(5))
  }


	def "http responds to requests from clients"() {
	given: "a simple HttpServer"

	//Listen on localhost using default impl (Netty) and assign a global codec to receive/reply String data
	def server = ReactiveNet.httpServer {
	  it.httpProcessor(CodecPreprocessor.string()).listen(0)
	}

	when: "the server is prepared"

	//prepare post request consumer on /test/* and capture the URL parameter "param"
{'fraction_non_alphanumeric': 0.09621735214650255, 'fraction_numerical': 0.004803362353647553, 'mean_word_length': 4.113584036838066, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7590367', 'n_tokens_mistral': 1946, 'n_tokens_neox': 1829, 'n_words': 708}